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



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

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

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



44
45
46
47
48
49
50
# File 'app/models/spree/address.rb', line 44

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) ⇒ Object



96
97
98
99
100
# File 'app/models/spree/address.rb', line 96

def ==(other)
  return false unless other&.respond_to?(:value_attributes)

  value_attributes == other.value_attributes
end

#active_merchant_hashObject

Generates an ActiveMerchant compatible address hash



111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/spree/address.rb', line 111

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



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

def clone
  self.class.new(value_attributes)
end

#empty?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'app/models/spree/address.rb', line 106

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

#full_nameObject



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

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

#iso_nameObject



52
53
54
55
56
57
58
# File 'app/models/spree/address.rb', line 52

def iso_name
  ActiveSupport::Deprecation.warn(<<-DEPRECATION, caller)
Address#iso_name is deprecated and will be removed in Spree 4.0.
Please use Address#country_iso_name instead
  DEPRECATION
  country_iso_name
end

#require_phone?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/spree/address.rb', line 124

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

#require_zipcode?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'app/models/spree/address.rb', line 128

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

#same_as(other) ⇒ Object



80
81
82
83
84
85
86
# File 'app/models/spree/address.rb', line 80

def same_as(other)
  ActiveSupport::Deprecation.warn(<<-EOS, caller)
    Address#same_as is deprecated and will be removed in Spree 4.0. Please use Address#== instead"
  EOS

  self == other
end

#same_as?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def same_as?(other)
  ActiveSupport::Deprecation.warn(<<-EOS, caller)
    Address#same_as? is deprecated and will be removed in Spree 4.0. Please use Address#== instead"
  EOS

  self == other
end

#state_name_textObject



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

def state_name_text
  state_name.present? ? state_name : state&.name
end

#state_textObject



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

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

#to_sObject



88
89
90
# File 'app/models/spree/address.rb', line 88

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

#value_attributesObject



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

def value_attributes
  attributes.except(*EXCLUDED_KEYS_FOR_COMPARISION)
end