Class: Spree::Address

Inherits:
Base
  • Object
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

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Class Method Details

.build_defaultObject



23
24
25
26
# File 'app/models/spree/address.rb', line 23

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



28
29
30
31
32
33
34
# File 'app/models/spree/address.rb', line 28

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



64
65
66
67
68
69
70
71
# File 'app/models/spree/address.rb', line 64

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_hashObject

Generates an ActiveMerchant compatible address hash



78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/spree/address.rb', line 78

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

#cloneObject



60
61
62
# File 'app/models/spree/address.rb', line 60

def clone
  self.class.new(self.attributes.except('id', 'updated_at', 'created_at'))
end

#empty?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/models/spree/address.rb', line 73

def empty?
  attributes.except('id', 'created_at', 'updated_at', 'country_id').all? { |_, v| v.nil? }
end

#full_nameObject

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



41
42
43
# File 'app/models/spree/address.rb', line 41

def full_name
  "#{firstname} #{lastname}".strip
end

#require_phone?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/spree/address.rb', line 91

def require_phone?
  true
end

#require_zipcode?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/spree/address.rb', line 95

def require_zipcode?
  true
end

#same_as?(other) ⇒ Boolean Also known as: same_as

Returns:

  • (Boolean)


49
50
51
52
# File 'app/models/spree/address.rb', line 49

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_textObject



45
46
47
# File 'app/models/spree/address.rb', line 45

def state_text
  state.try(:abbr) || state.try(:name) || state_name
end

#to_sObject



56
57
58
# File 'app/models/spree/address.rb', line 56

def to_s
  "#{full_name}: #{address1}"
end