Class: Spree::Address
- Defined in:
- app/models/spree/address.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other_address) ⇒ Boolean
True if the two addresses have the same address fields.
-
#active_merchant_hash ⇒ Hash
An ActiveMerchant compatible address hash.
-
#clone ⇒ Spree::Address
A new address that is the same_as? this address.
-
#empty? ⇒ Boolean
True if the order is missing all of the address fields are nil.
-
#full_name ⇒ String
The full name on this address.
-
#require_phone? ⇒ true
Whether or not the address requires a phone number to be valid.
-
#require_zipcode? ⇒ true
Whether or not the address requires a zipcode to be valid.
-
#same_as?(other) ⇒ Boolean
(also: #same_as)
True if this fields on this address match the fields on the other address.
-
#state_text ⇒ String
A string representation of this state.
-
#to_s ⇒ String
The full name on the address followed by the first line of the address.
Methods inherited from Base
Methods included from Preferences::Preferable
#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Class Method Details
.build_default ⇒ Object
24 25 26 27 |
# File 'app/models/spree/address.rb', line 24 def self.build_default country = Spree::Country.find(Spree::Config[:default_country_id]) rescue Spree::Country.first new(country: country) end |
.default(user = nil, kind = "bill") ⇒ Object
29 30 31 32 33 34 35 |
# File 'app/models/spree/address.rb', line 29 def self.default(user = nil, kind = "bill") if user && user_address = user.send(:"#{kind}_address") user_address.dup else build_default end end |
Instance Method Details
#==(other_address) ⇒ Boolean
This compares the addresses based on only the fields that make up the logical “address” and excludes their order IDs. Use #same_as? to include the order IDs in the comparison
Returns true if the two addresses have the same address fields.
73 74 75 76 77 78 79 80 |
# File 'app/models/spree/address.rb', line 73 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 ⇒ Hash
Returns an ActiveMerchant compatible address hash.
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/spree/address.rb', line 89 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 ⇒ Spree::Address
Returns a new address that is the same_as? this address.
64 65 66 67 |
# File 'app/models/spree/address.rb', line 64 def clone ActiveSupport::Deprecation.warn "Spree::Address.clone is deprecated and may be removed from future releases, Use Spree::Address.dup instead", caller self.dup end |
#empty? ⇒ Boolean
Returns true if the order is missing all of the address fields are nil.
84 85 86 |
# File 'app/models/spree/address.rb', line 84 def empty? attributes.except('id', 'created_at', 'updated_at', 'order_id', 'country_id').all? { |_, v| v.nil? } end |
#full_name ⇒ String
Returns the full name on this address.
38 39 40 |
# File 'app/models/spree/address.rb', line 38 def full_name "#{firstname} #{lastname}".strip end |
#require_phone? ⇒ true
Remove this from the public API if possible.
Returns whether or not the address requires a phone number to be valid.
105 106 107 |
# File 'app/models/spree/address.rb', line 105 def require_phone? true end |
#require_zipcode? ⇒ true
Remove this from the public API if possible.
Returns whether or not the address requires a zipcode to be valid.
111 112 113 |
# File 'app/models/spree/address.rb', line 111 def require_zipcode? true end |
#same_as?(other) ⇒ Boolean Also known as: same_as
Returns true if this fields on this address match the fields on the other address.
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 ⇒ String
Returns a string representation of this state.
43 44 45 |
# File 'app/models/spree/address.rb', line 43 def state_text state.try(:abbr) || state.try(:name) || state_name end |
#to_s ⇒ String
Returns the full name on the address followed by the first line of the address.
59 60 61 |
# File 'app/models/spree/address.rb', line 59 def to_s "#{full_name}: #{address1}" end |