Class: Spree::Address
- Inherits:
-
Base
- Object
- ApplicationRecord
- Base
- Spree::Address
show all
- Includes:
- Metadata, 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)
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
belongs_to_required_by_default, for_store, has_many_inversing, json_api_columns, json_api_permitted_attributes, json_api_type, page, spree_base_scopes, spree_base_uniqueness_scope
#clear_preferences, #default_preferences, #defined_preferences, #deprecated_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_deprecated, #preference_type, #set_preference
Class Method Details
.build_default ⇒ Object
71
72
73
74
75
76
77
|
# File 'app/models/spree/address.rb', line 71
def self.build_default
Spree::Deprecation.warn(<<-DEPRECATION, caller)
`Address#build_default` is deprecated and will be removed in Spree 5.0.
Please use standard rails `Address.new(country: current_store.default_country)`
DEPRECATION
new(country: Spree::Country.default)
end
|
.default(user = nil, kind = 'bill') ⇒ Object
79
80
81
82
83
84
85
86
87
88
|
# File 'app/models/spree/address.rb', line 79
def self.default(user = nil, kind = 'bill')
Spree::Deprecation.warn(<<-DEPRECATION, caller)
`Address#default` is deprecated and will be removed in Spree 5.0.
DEPRECATION
if user && user_address = user.public_send(:"#{kind}_address")
user_address.clone
else
build_default
end
end
|
.required_fields ⇒ Object
90
91
92
93
94
|
# File 'app/models/spree/address.rb', line 90
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
123
124
125
126
127
|
# File 'app/models/spree/address.rb', line 123
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
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'app/models/spree/address.rb', line 138
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
|
#can_be_deleted? ⇒ Boolean
163
164
165
|
# File 'app/models/spree/address.rb', line 163
def can_be_deleted?
shipments.empty? && !Order.where('bill_address_id = ? OR ship_address_id = ?', id, id).exists?
end
|
#check ⇒ Object
167
168
169
170
171
|
# File 'app/models/spree/address.rb', line 167
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
119
120
121
|
# File 'app/models/spree/address.rb', line 119
def clone
self.class.new(value_attributes)
end
|
#destroy ⇒ Object
173
174
175
176
177
178
179
180
|
# File 'app/models/spree/address.rb', line 173
def destroy
if can_be_deleted?
super
else
update_column :deleted_at, Time.current
assign_new_default_address_to_user
end
end
|
#editable? ⇒ Boolean
159
160
161
|
# File 'app/models/spree/address.rb', line 159
def editable?
new_record? || (shipments.empty? && !Order.complete.where('bill_address_id = ? OR ship_address_id = ?', id, id).exists?)
end
|
#empty? ⇒ Boolean
133
134
135
|
# File 'app/models/spree/address.rb', line 133
def empty?
attributes.except('id', 'created_at', 'updated_at', 'country_id').all? { |_, v| v.nil? }
end
|
#full_name ⇒ Object
96
97
98
|
# File 'app/models/spree/address.rb', line 96
def full_name
"#{firstname} #{lastname}".strip
end
|
#require_phone? ⇒ Boolean
151
152
153
|
# File 'app/models/spree/address.rb', line 151
def require_phone?
Spree::Config[:address_requires_phone]
end
|
#require_zipcode? ⇒ Boolean
155
156
157
|
# File 'app/models/spree/address.rb', line 155
def require_zipcode?
country ? country.zipcode_required? : true
end
|
#state_name_text ⇒ Object
104
105
106
|
# File 'app/models/spree/address.rb', line 104
def state_name_text
state_name.present? ? state_name : state&.name
end
|
#state_text ⇒ Object
100
101
102
|
# File 'app/models/spree/address.rb', line 100
def state_text
state.try(:abbr) || state.try(:name) || state_name
end
|
#to_s ⇒ Object
108
109
110
111
112
113
114
115
116
117
|
# File 'app/models/spree/address.rb', line 108
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
|
#value_attributes ⇒ Object