Class: Spree::Address
- Inherits:
-
Object
- Object
- Spree::Address
- Includes:
- Metadata, Metafields, Security::Addresses, Webhooks::HasWebhooks
- Defined in:
- app/models/spree/address.rb
Constant Summary collapse
- STATES_REQUIRED =
The required states listed below match those used by PayPal and Shopify.
[ 'AU', 'AE', 'BR', 'CA', 'CN', 'ES', 'HK', 'IE', 'IN', 'IT', 'MY', 'MX', 'NZ', 'PT', 'RO', 'TH', 'US', 'ZA' ].freeze
- ADDRESS_FIELDS =
we’re not freezing this on purpose so developers can extend and manage those attributes depending of the logic of their applications
%w(firstname lastname company address1 address2 city state zipcode country phone)
- EXCLUDED_KEYS_FOR_COMPARISON =
%w(id updated_at created_at deleted_at label user_id public_metadata private_metadata)
- FIELDS_TO_NORMALIZE =
%w(firstname lastname phone alternative_phone company address1 address2 city zipcode)
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#active_merchant_hash ⇒ Object
Generates an ActiveMerchant compatible address hash.
- #address_validators ⇒ Object
- #async_geocode ⇒ Object
- #can_be_deleted? ⇒ Boolean
- #check ⇒ Object
- #clone ⇒ Object
- #destroy ⇒ Object
- #editable? ⇒ Boolean
- #empty? ⇒ Boolean
- #first_name ⇒ Object
- #first_name=(value) ⇒ Object
- #full_name ⇒ Object
- #geocoder_address ⇒ Object
- #last_name ⇒ Object
- #last_name=(value) ⇒ Object
- #require_company? ⇒ Boolean
- #require_name? ⇒ Boolean
- #require_phone? ⇒ Boolean
- #require_street? ⇒ Boolean
- #require_zipcode? ⇒ Boolean
- #show_company_address_field? ⇒ Boolean
- #state_name_text ⇒ Object
- #state_text ⇒ Object
- #street ⇒ Object
- #to_s ⇒ Object
- #user_default_billing? ⇒ Boolean
- #user_default_shipping? ⇒ Boolean
- #value_attributes ⇒ Object
Class Method Details
.required_fields ⇒ Object
92 93 94 95 96 |
# File 'app/models/spree/address.rb', line 92 def self.required_fields Spree::Address.validators.map do |v| v.is_a?(ActiveModel::Validations::PresenceValidator) ? v.attributes : [] end.flatten end |
Instance Method Details
#==(other) ⇒ Object
153 154 155 156 157 |
# File 'app/models/spree/address.rb', line 153 def ==(other) return false unless other&.respond_to?(:value_attributes) value_attributes == other.value_attributes end |
#active_merchant_hash ⇒ Object
Generates an ActiveMerchant compatible address hash
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/models/spree/address.rb', line 168 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 |
#address_validators ⇒ Object
78 79 80 81 82 |
# File 'app/models/spree/address.rb', line 78 def address_validators Spree.validators.addresses.each do |validator| validates_with validator end end |
#async_geocode ⇒ Object
231 232 233 |
# File 'app/models/spree/address.rb', line 231 def async_geocode Spree::Addresses::GeocodeAddressJob.perform_later(id) if should_geocode? end |
#can_be_deleted? ⇒ Boolean
211 212 213 |
# File 'app/models/spree/address.rb', line 211 def can_be_deleted? shipments.empty? && Order.complete.where('bill_address_id = ? OR ship_address_id = ?', id, id).none? end |
#check ⇒ Object
215 216 217 218 219 |
# File 'app/models/spree/address.rb', line 215 def check attrs = attributes.except('id', 'updated_at', 'created_at') the_same_address = user&.addresses&.find_by(attrs) the_same_address || self end |
#clone ⇒ Object
149 150 151 |
# File 'app/models/spree/address.rb', line 149 def clone self.class.new(value_attributes) end |
#destroy ⇒ Object
221 222 223 224 225 226 227 228 229 |
# File 'app/models/spree/address.rb', line 221 def destroy assign_new_default_address_to_user if can_be_deleted? super else update_column :deleted_at, Time.current end end |
#editable? ⇒ Boolean
207 208 209 |
# File 'app/models/spree/address.rb', line 207 def editable? new_record? || Order.complete.where('bill_address_id = ? OR ship_address_id = ?', id, id).none? end |
#empty? ⇒ Boolean
163 164 165 |
# File 'app/models/spree/address.rb', line 163 def empty? attributes.except('id', 'created_at', 'updated_at', 'country_id').all? { |_, v| v.nil? } end |
#first_name ⇒ Object
106 107 108 |
# File 'app/models/spree/address.rb', line 106 def first_name firstname end |
#first_name=(value) ⇒ Object
110 111 112 |
# File 'app/models/spree/address.rb', line 110 def first_name=(value) self.firstname = value end |
#full_name ⇒ Object
122 123 124 |
# File 'app/models/spree/address.rb', line 122 def full_name "#{firstname} #{lastname}".strip end |
#geocoder_address ⇒ Object
235 236 237 |
# File 'app/models/spree/address.rb', line 235 def geocoder_address @geocoder_address ||= [street, city, state_text, country.to_s].compact.map(&:strip).join(', ') end |
#last_name ⇒ Object
114 115 116 |
# File 'app/models/spree/address.rb', line 114 def last_name lastname end |
#last_name=(value) ⇒ Object
118 119 120 |
# File 'app/models/spree/address.rb', line 118 def last_name=(value) self.lastname = value end |
#require_company? ⇒ Boolean
195 196 197 |
# File 'app/models/spree/address.rb', line 195 def require_company? false end |
#require_name? ⇒ Boolean
191 192 193 |
# File 'app/models/spree/address.rb', line 191 def require_name? !quick_checkout end |
#require_phone? ⇒ Boolean
181 182 183 184 185 |
# File 'app/models/spree/address.rb', line 181 def require_phone? # We want to collect phone number for quick checkout but not to validate it # as it's not available before payment by browser. !quick_checkout && Spree::Config[:address_requires_phone] end |
#require_street? ⇒ Boolean
199 200 201 |
# File 'app/models/spree/address.rb', line 199 def require_street? !quick_checkout end |
#require_zipcode? ⇒ Boolean
187 188 189 |
# File 'app/models/spree/address.rb', line 187 def require_zipcode? !quick_checkout && (country ? country.zipcode_required? : true) end |
#show_company_address_field? ⇒ Boolean
203 204 205 |
# File 'app/models/spree/address.rb', line 203 def show_company_address_field? Spree::Store.current.prefers_company_field_enabled? end |
#state_name_text ⇒ Object
130 131 132 |
# File 'app/models/spree/address.rb', line 130 def state_name_text state_name.present? ? state_name : state&.name end |
#state_text ⇒ Object
126 127 128 |
# File 'app/models/spree/address.rb', line 126 def state_text state.try(:abbr) || state.try(:name) || state_name end |
#street ⇒ Object
134 135 136 |
# File 'app/models/spree/address.rb', line 134 def street [address1, address2].join(' ') end |
#to_s ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'app/models/spree/address.rb', line 138 def to_s [ full_name, company, address1, address2, "#{city}, #{state_text} #{zipcode}", country.to_s ].reject(&:blank?).map { |attribute| ERB::Util.html_escape(attribute) }.join('<br/>') end |
#user_default_billing? ⇒ Boolean
98 99 100 |
# File 'app/models/spree/address.rb', line 98 def user_default_billing? user.present? && id == user.bill_address_id end |
#user_default_shipping? ⇒ Boolean
102 103 104 |
# File 'app/models/spree/address.rb', line 102 def user_default_shipping? user.present? && id == user.ship_address_id end |
#value_attributes ⇒ Object
159 160 161 |
# File 'app/models/spree/address.rb', line 159 def value_attributes attributes.except(*EXCLUDED_KEYS_FOR_COMPARISON) end |