Class: Spree::Address

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/address.rb

Constant Summary collapse

EXCLUDED_KEYS_FOR_COMPARISION =

we’re not freezing this on purpose so developers can extend and manage those attributes depending of the logic of their applications

%w(id updated_at created_at)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

belongs_to_required_by_default, 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



37
38
39
# File 'app/models/spree/address.rb', line 37

def self.build_default
  new(country: Spree::Country.default)
end

.default(user = nil, kind = 'bill') ⇒ Object



41
42
43
44
45
46
47
# File 'app/models/spree/address.rb', line 41

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



72
73
74
75
76
77
78
79
# File 'app/models/spree/address.rb', line 72

def ==(other_address)
  self_attrs = 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



86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/spree/address.rb', line 86

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



68
69
70
# File 'app/models/spree/address.rb', line 68

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

#empty?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/spree/address.rb', line 81

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

#full_nameObject



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

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

#require_phone?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/models/spree/address.rb', line 99

def require_phone?
  Spree::Config[:address_requires_phone]
end

#require_zipcode?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/spree/address.rb', line 103

def require_zipcode?
  country ? country.zipcode_required? : true
end

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

Returns:

  • (Boolean)


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

def same_as?(other)
  return false if other.nil?
  attributes.except(*EXCLUDED_KEYS_FOR_COMPARISION) == other.attributes.except(*EXCLUDED_KEYS_FOR_COMPARISION)
end

#state_textObject



53
54
55
# File 'app/models/spree/address.rb', line 53

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

#to_sObject



64
65
66
# File 'app/models/spree/address.rb', line 64

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