Class: Spree::Address
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::Address
- Defined in:
- app/models/spree/address.rb
Class Method Summary collapse
Instance Method Summary collapse
- #==(other_address) ⇒ Object
-
#active_merchant_hash ⇒ Object
Generates an ActiveMerchant compatible address hash.
- #clone ⇒ Object
- #empty? ⇒ Boolean
-
#full_name ⇒ Object
Can modify an address if it’s not been used in an order (but checkouts controller has finer control) def editable? new_record? || (shipments.empty? && checkouts.empty?) end.
-
#phone_validate ⇒ Object
Disconnected since there’s no code to display error messages yet OR matching client-side validation.
- #same_as?(other) ⇒ Boolean (also: #same_as)
- #state_text ⇒ Object
- #to_s ⇒ Object
- #zone ⇒ Object
- #zones ⇒ Object
Class Method Details
Instance Method Details
#==(other_address) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'app/models/spree/address.rb', line 65 def ==(other_address) self_attrs = self.attributes other_attrs = other_address.respond_to?(:attributes) ? other_address.attributes : {} [self_attrs, other_attrs].each { |attrs| attrs.except!('id', 'created_at', 'updated_at', 'order_id') } self_attrs == other_attrs end |
#active_merchant_hash ⇒ Object
Generates an ActiveMerchant compatible address hash
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/models/spree/address.rb', line 79 def active_merchant_hash { :name => full_name, :address1 => address1, :address2 => address2, :city => city, :state => state_text, :zip => zipcode, :country => country.try(:iso), :phone => phone } end |
#clone ⇒ Object
61 62 63 |
# File 'app/models/spree/address.rb', line 61 def clone self.class.new(self.attributes.except('id', 'updated_at', 'created_at')) end |
#empty? ⇒ Boolean
74 75 76 |
# File 'app/models/spree/address.rb', line 74 def empty? attributes.except('id', 'created_at', 'updated_at', 'order_id', 'country_id').all? { |_, v| v.nil? } end |
#full_name ⇒ Object
Can modify an address if it’s not been used in an order (but checkouts controller has finer control) def editable?
new_record? || (shipments.empty? && checkouts.empty?)
end
34 35 36 |
# File 'app/models/spree/address.rb', line 34 def full_name "#{firstname} #{lastname}".strip end |
#phone_validate ⇒ Object
Disconnected since there’s no code to display error messages yet OR matching client-side validation
17 18 19 20 21 22 |
# File 'app/models/spree/address.rb', line 17 def phone_validate return if phone.blank? n_digits = phone.scan(/[0-9]/).size valid_chars = (phone =~ /^[-+()\/\s\d]+$/) errors.add :phone, :invalid unless (n_digits > 5 && valid_chars) end |
#same_as?(other) ⇒ Boolean Also known as: same_as
50 51 52 53 |
# File 'app/models/spree/address.rb', line 50 def same_as?(other) return false if other.nil? attributes.except('id', 'updated_at', 'created_at') == other.attributes.except('id', 'updated_at', 'created_at') end |
#state_text ⇒ Object
38 39 40 |
# File 'app/models/spree/address.rb', line 38 def state_text state.nil? ? state_name : (state.abbr.blank? ? state.name : state.abbr) end |
#to_s ⇒ Object
57 58 59 |
# File 'app/models/spree/address.rb', line 57 def to_s "#{full_name}: #{address1}" end |
#zone ⇒ Object
42 43 44 |
# File 'app/models/spree/address.rb', line 42 def zone (state && state.zone) || (country && country.zone) end |