Class: Spree::Address

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

Overview

‘Spree::Address` provides the foundational ActiveRecord model for recording and validating address information for `Spree::Order`, `Spree::Shipment`, `Spree::UserAddress`, and `Spree::Carton`.

Constant Summary collapse

DB_ONLY_ATTRS =
%w(id updated_at created_at)
TAXATION_ATTRS =
%w(state_id country_id zipcode)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

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

Class Method Details

.build_default(*args, &block) ⇒ Address

Returns an address with default attributes.

Returns:

  • (Address)

    an address with default attributes



34
35
36
# File 'app/models/spree/address.rb', line 34

def self.build_default(*args, &block)
  where(country: Spree::Country.default).build(*args, &block)
end

.factory(attributes) ⇒ Address

Returns an equal address already in the database or a newly created one.

Returns:

  • (Address)

    an equal address already in the database or a newly created one



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

def self.factory(attributes)
  full_attributes = value_attributes(column_defaults, new(attributes).attributes)
  find_or_initialize_by(full_attributes)
end

.immutable_merge(existing_address, new_attributes) ⇒ Address

@note, this may return existing_address if there are no changes to value equality

Returns:

  • (Address)

    address from existing address plus new_attributes as diff



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/spree/address.rb', line 46

def self.immutable_merge(existing_address, new_attributes)
  # Ensure new_attributes is a sanitized hash
  new_attributes = sanitize_for_mass_assignment(new_attributes)

  return factory(new_attributes) if existing_address.nil?

  merged_attributes = value_attributes(existing_address.attributes, new_attributes)
  new_address = factory(merged_attributes)
  if existing_address == new_address
    existing_address
  else
    new_address
  end
end

.value_attributes(base_attributes, merge_attributes = {}) ⇒ Hash

Returns hash of attributes contributing to value equality with optional merge.

Returns:

  • (Hash)

    hash of attributes contributing to value equality with optional merge



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

def self.value_attributes(base_attributes, merge_attributes = {})
  base = base_attributes.stringify_keys.merge(merge_attributes.stringify_keys)

  # TODO: Deprecate these aliased attributes
  base['firstname'] = base['first_name'] if base.key?('first_name')
  base['lastname'] = base['last_name'] if base.key?('last_name')

  excluded_attributes = DB_ONLY_ATTRS + %w(first_name last_name)

  base.except(*excluded_attributes)
end

Instance Method Details

#==(other_address) ⇒ Boolean

Note:

This compares the addresses based on only the fields that make up the logical “address” and excludes the database specific fields (id, created_at, updated_at).

Returns true if the two addresses have the same address fields.

Returns:

  • (Boolean)

    true if the two addresses have the same address fields



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

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

#active_merchant_hashHash

Returns an ActiveMerchant compatible address hash.

Returns:

  • (Hash)

    an ActiveMerchant compatible address hash



131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/spree/address.rb', line 131

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

#blank?Boolean

This exists because the default Object#blank?, checks empty? if it is defined, and we have defined empty. This should be removed once empty? is removed

Returns:

  • (Boolean)


126
127
128
# File 'app/models/spree/address.rb', line 126

def blank?
  false
end

#country_isoObject



171
172
173
# File 'app/models/spree/address.rb', line 171

def country_iso
  country && country.iso
end

#country_iso=(iso) ⇒ Country

Returns setter that sets self.country to the Country with a matching 2 letter iso.

Parameters:

  • iso (String)

    2 letter Country ISO

Returns:

  • (Country)

    setter that sets self.country to the Country with a matching 2 letter iso

Raises:

  • (ActiveRecord::RecordNotFound)

    if country with the iso doesn’t exist



167
168
169
# File 'app/models/spree/address.rb', line 167

def country_iso=(iso)
  self.country = Spree::Country.find_by!(iso: iso)
end

#empty?Boolean

Deprecated.

Do not use this

Returns:

  • (Boolean)


118
119
120
121
# File 'app/models/spree/address.rb', line 118

def empty?
  Spree::Deprecation.warn("Address#empty? is deprecated.", caller)
  attributes.except('id', 'created_at', 'updated_at', 'country_id').all? { |_, value| value.nil? }
end

#full_nameString

Returns the full name on this address.

Returns:

  • (String)

    the full name on this address



84
85
86
# File 'app/models/spree/address.rb', line 84

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

#readonly?Boolean

This is set in order to preserve immutability of Addresses. Use #dup to create new records as required, but it probably won’t be required as often as you think. Since addresses do not change, you won’t accidentally alter historical data.

Returns:

  • (Boolean)


160
161
162
# File 'app/models/spree/address.rb', line 160

def readonly?
  persisted?
end

#require_phone?true

TODO:

Remove this from the public API if possible.

Returns whether or not the address requires a phone number to be valid.

Returns:

  • (true)

    whether or not the address requires a phone number to be valid



147
148
149
# File 'app/models/spree/address.rb', line 147

def require_phone?
  true
end

#require_zipcode?true

TODO:

Remove this from the public API if possible.

Returns whether or not the address requires a zipcode to be valid.

Returns:

  • (true)

    whether or not the address requires a zipcode to be valid



153
154
155
# File 'app/models/spree/address.rb', line 153

def require_zipcode?
  true
end

#same_as(other_address) ⇒ Object

Deprecated.

Do not use this. Use Address.== instead.



112
113
114
115
# File 'app/models/spree/address.rb', line 112

def same_as(other_address)
  Spree::Deprecation.warn("Address#same_as is deprecated. It's equivalent to Address.==", caller)
  self == other_address
end

#same_as?(other_address) ⇒ Boolean

Deprecated.

Do not use this. Use Address.== instead.

Returns:

  • (Boolean)


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

def same_as?(other_address)
  Spree::Deprecation.warn("Address#same_as? is deprecated. It's equivalent to Address.==", caller)
  self == other_address
end

#state_textString

Returns a string representation of this state.

Returns:

  • (String)

    a string representation of this state



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

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

#taxation_attributesObject



79
80
81
# File 'app/models/spree/address.rb', line 79

def taxation_attributes
  self.class.value_attributes(attributes.slice(*TAXATION_ATTRS))
end

#to_sObject



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

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

#value_attributesHash

Returns hash of attributes contributing to value equality.

Returns:

  • (Hash)

    hash of attributes contributing to value equality



75
76
77
# File 'app/models/spree/address.rb', line 75

def value_attributes
  self.class.value_attributes(attributes)
end