Class: Spree::Address
- Extended by:
- ActiveModel::ForbiddenAttributesProtection
- Defined in:
- app/models/spree/address.rb
Overview
‘Spree::Address` provides the foundational ActiveRecord model for recording and validating address information for `Spree::Order`, `Spree::Shipment`, `Spree::UserAddress`, and `Spree::Carton`.
Constant Summary collapse
- DB_ONLY_ATTRS =
%w(id updated_at created_at)
- TAXATION_ATTRS =
%w(state_id country_id zipcode)
Class Method Summary collapse
- .build_default ⇒ Object
-
.factory(attributes) ⇒ Address
An equal address already in the database or a newly created one.
-
.immutable_merge(existing_address, new_attributes) ⇒ Address
@note, this may return existing_address if there are no changes to value equality.
-
.value_attributes(base_attributes, merge_attributes = nil) ⇒ Hash
Hash of attributes contributing to value equality with optional merge.
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.
-
#blank? ⇒ Boolean
This exists because the default Object#blank?, checks empty? if it is defined, and we have defined empty.
- #country_iso ⇒ Object
-
#country_iso=(iso) ⇒ Country
Setter that sets self.country to the Country with a matching 2 letter iso.
-
#empty? ⇒ Boolean
deprecated
Deprecated.
Do not use this
-
#full_name ⇒ String
The full name on this address.
-
#readonly? ⇒ Boolean
This is set in order to preserve immutability of Addresses.
-
#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_address) ⇒ Object
deprecated
Deprecated.
Do not use this. Use Address.== instead.
-
#same_as?(other_address) ⇒ Boolean
deprecated
Deprecated.
Do not use this. Use Address.== instead.
-
#state_text ⇒ String
A string representation of this state.
- #taxation_attributes ⇒ Object
- #to_s ⇒ Object
-
#value_attributes ⇒ Hash
Hash of attributes contributing to value equality.
Methods inherited from Base
display_includes, #initialize_preference_defaults, page, preference
Methods included from Preferences::Preferable
#admin_form_preference_names, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Class Method Details
.build_default ⇒ Object
31 32 33 |
# File 'app/models/spree/address.rb', line 31 def self.build_default new(country: Spree::Country.default) end |
.factory(attributes) ⇒ Address
Returns an equal address already in the database or a newly created one.
36 37 38 39 |
# File 'app/models/spree/address.rb', line 36 def self.factory(attributes) full_attributes = value_attributes(column_defaults, new(attributes).attributes) find_or_initialize_by(full_attributes) end |
.immutable_merge(existing_address, new_attributes) ⇒ Address
@note, this may return existing_address if there are no changes to value equality
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/spree/address.rb', line 43 def self.immutable_merge(existing_address, new_attributes) # Ensure new_attributes is a sanitized hash new_attributes = sanitize_for_mass_assignment(new_attributes) return factory(new_attributes) if existing_address.nil? merged_attributes = value_attributes(existing_address.attributes, new_attributes) new_address = factory(merged_attributes) if existing_address == new_address existing_address else new_address end end |
.value_attributes(base_attributes, merge_attributes = nil) ⇒ Hash
Returns hash of attributes contributing to value equality with optional merge.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/spree/address.rb', line 59 def self.value_attributes(base_attributes, merge_attributes = nil) # dup because we may modify firstname/lastname. base = base_attributes.dup base.stringify_keys! if merge_attributes base.merge!(merge_attributes.stringify_keys) end # TODO: Deprecate these aliased attributes base['firstname'] = base.delete('first_name') if base.key?('first_name') base['lastname'] = base.delete('last_name') if base.key?('last_name') base.except!(*DB_ONLY_ATTRS) end |
Instance Method Details
#==(other_address) ⇒ Boolean
This compares the addresses based on only the fields that make up the logical “address” and excludes the database specific fields (id, created_at, updated_at).
Returns true if the two addresses have the same address fields.
102 103 104 105 |
# File 'app/models/spree/address.rb', line 102 def ==(other_address) return false unless other_address && other_address.respond_to?(:value_attributes) value_attributes == other_address.value_attributes end |
#active_merchant_hash ⇒ Hash
Returns an ActiveMerchant compatible address hash.
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/spree/address.rb', line 133 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 |
#blank? ⇒ Boolean
This exists because the default Object#blank?, checks empty? if it is defined, and we have defined empty. This should be removed once empty? is removed
128 129 130 |
# File 'app/models/spree/address.rb', line 128 def blank? false end |
#country_iso ⇒ Object
173 174 175 |
# File 'app/models/spree/address.rb', line 173 def country_iso country && country.iso end |
#country_iso=(iso) ⇒ Country
Returns setter that sets self.country to the Country with a matching 2 letter iso.
169 170 171 |
# File 'app/models/spree/address.rb', line 169 def country_iso=(iso) self.country = Spree::Country.find_by!(iso: iso) end |
#empty? ⇒ Boolean
Do not use this
120 121 122 123 |
# File 'app/models/spree/address.rb', line 120 def empty? Spree::Deprecation.warn("Address#empty? is deprecated.", caller) attributes.except('id', 'created_at', 'updated_at', 'country_id').all? { |_, v| v.nil? } end |
#full_name ⇒ String
Returns the full name on this address.
86 87 88 |
# File 'app/models/spree/address.rb', line 86 def full_name "#{firstname} #{lastname}".strip end |
#readonly? ⇒ Boolean
This is set in order to preserve immutability of Addresses. Use #dup to create new records as required, but it probably won’t be required as often as you think. Since addresses do not change, you won’t accidentally alter historical data.
162 163 164 |
# File 'app/models/spree/address.rb', line 162 def readonly? persisted? 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.
149 150 151 |
# File 'app/models/spree/address.rb', line 149 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.
155 156 157 |
# File 'app/models/spree/address.rb', line 155 def require_zipcode? true end |
#same_as(other_address) ⇒ Object
Do not use this. Use Address.== instead.
114 115 116 117 |
# File 'app/models/spree/address.rb', line 114 def same_as(other_address) Spree::Deprecation.warn("Address#same_as is deprecated. It's equivalent to Address.==", caller) self == other_address end |
#same_as?(other_address) ⇒ Boolean
Do not use this. Use Address.== instead.
108 109 110 111 |
# File 'app/models/spree/address.rb', line 108 def same_as?(other_address) Spree::Deprecation.warn("Address#same_as? is deprecated. It's equivalent to Address.==", caller) self == other_address end |
#state_text ⇒ String
Returns a string representation of this state.
91 92 93 |
# File 'app/models/spree/address.rb', line 91 def state_text state.try(:abbr) || state.try(:name) || state_name end |
#taxation_attributes ⇒ Object
81 82 83 |
# File 'app/models/spree/address.rb', line 81 def taxation_attributes self.class.value_attributes(attributes.slice(*TAXATION_ATTRS)) end |
#to_s ⇒ Object
95 96 97 |
# File 'app/models/spree/address.rb', line 95 def to_s "#{full_name}: #{address1}" end |
#value_attributes ⇒ Hash
Returns hash of attributes contributing to value equality.
77 78 79 |
# File 'app/models/spree/address.rb', line 77 def value_attributes self.class.value_attributes(attributes) end |