Class: Spree::Address
- Inherits:
-
Base
- Object
- ApplicationRecord
- Base
- Spree::Address
show all
- Defined in:
- app/models/spree/address.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
page, spree_base_scopes
#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Class Method Details
.build_default ⇒ Object
33
34
35
36
|
# File 'app/models/spree/address.rb', line 33
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
38
39
40
41
42
43
44
|
# File 'app/models/spree/address.rb', line 38
def self.default(user = nil, kind = "bill")
if user && user_address = user.public_send(:"#{kind}_address")
user_address.clone
else
build_default
end
end
|
Instance Method Details
#==(other_address) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'app/models/spree/address.rb', line 74
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') }
self_attrs == other_attrs
end
|
#active_merchant_hash ⇒ Object
Generates an ActiveMerchant compatible address hash
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'app/models/spree/address.rb', line 88
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
70
71
72
|
# File 'app/models/spree/address.rb', line 70
def clone
self.class.new(self.attributes.except('id', 'updated_at', 'created_at'))
end
|
#empty? ⇒ Boolean
83
84
85
|
# File 'app/models/spree/address.rb', line 83
def empty?
attributes.except('id', 'created_at', 'updated_at', '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
51
52
53
|
# File 'app/models/spree/address.rb', line 51
def full_name
"#{firstname} #{lastname}".strip
end
|
#require_phone? ⇒ Boolean
101
102
103
|
# File 'app/models/spree/address.rb', line 101
def require_phone?
true
end
|
#require_zipcode? ⇒ Boolean
105
106
107
|
# File 'app/models/spree/address.rb', line 105
def require_zipcode?
country ? country.zipcode_required? : true
end
|
#same_as?(other) ⇒ Boolean
Also known as:
same_as
59
60
61
62
|
# File 'app/models/spree/address.rb', line 59
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
55
56
57
|
# File 'app/models/spree/address.rb', line 55
def state_text
state.try(:abbr) || state.try(:name) || state_name
end
|
#to_s ⇒ Object
66
67
68
|
# File 'app/models/spree/address.rb', line 66
def to_s
"#{full_name}: #{address1}"
end
|