Class: Spree::Address
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::Address
- Defined in:
- app/models/spree/address.rb
Class Method Summary collapse
- .build_default ⇒ Object
-
.default(user = nil, kind = "bill") ⇒ 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.
Instance Method Summary collapse
- #==(other_address) ⇒ Object
-
#active_merchant_hash ⇒ Object
Generates an ActiveMerchant compatible address hash.
- #clone ⇒ Object
- #empty? ⇒ Boolean
- #full_name ⇒ Object
- #same_as?(other) ⇒ Boolean (also: #same_as)
- #state_text ⇒ Object
- #to_s ⇒ Object
Class Method Details
.build_default ⇒ Object
17 18 19 20 |
# File 'app/models/spree/address.rb', line 17 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
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
27 28 29 30 31 32 33 |
# File 'app/models/spree/address.rb', line 27 def self.default(user = nil, kind = "bill") if user user.send(:"#{kind}_address") || build_default else build_default end end |
Instance Method Details
#==(other_address) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'app/models/spree/address.rb', line 58 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 ⇒ Object
Generates an ActiveMerchant compatible address hash
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/spree/address.rb', line 72 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
54 55 56 |
# File 'app/models/spree/address.rb', line 54 def clone self.class.new(self.attributes.except('id', 'updated_at', 'created_at')) end |
#empty? ⇒ Boolean
67 68 69 |
# File 'app/models/spree/address.rb', line 67 def empty? attributes.except('id', 'created_at', 'updated_at', 'order_id', 'country_id').all? { |_, v| v.nil? } end |
#full_name ⇒ Object
35 36 37 |
# File 'app/models/spree/address.rb', line 35 def full_name "#{firstname} #{lastname}".strip end |
#same_as?(other) ⇒ Boolean Also known as: same_as
43 44 45 46 |
# File 'app/models/spree/address.rb', line 43 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
39 40 41 |
# File 'app/models/spree/address.rb', line 39 def state_text state.try(:abbr) || state.try(:name) || state_name end |
#to_s ⇒ Object
50 51 52 |
# File 'app/models/spree/address.rb', line 50 def to_s "#{full_name}: #{address1}" end |