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

Methods included from Preferences::Preferable

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

Class Method Details

.build_defaultObject



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

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



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

def self.default(user = nil, kind = "bill")
  if user && user_address = user.send(:"#{kind}_address")
    user_address.dup
  else
    build_default
  end
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 their order IDs. Use #same_as? to include the order IDs in the comparison

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

Returns:

  • (Boolean)

    true if the two addresses have the same address fields



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

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_hashHash

Returns an ActiveMerchant compatible address hash.

Returns:

  • (Hash)

    an ActiveMerchant compatible address hash



89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/spree/address.rb', line 89

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

#cloneSpree::Address

Returns a new address that is the same_as? this address.

Returns:

  • (Spree::Address)

    a new address that is the same_as? this address



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

def clone
  ActiveSupport::Deprecation.warn "Spree::Address.clone is deprecated and may be removed from future releases, Use Spree::Address.dup instead", caller
  self.dup
end

#empty?Boolean

Returns true if the order is missing all of the address fields are nil.

Returns:

  • (Boolean)

    true if the order is missing all of the address fields are nil



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

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

#full_nameString

Returns the full name on this address.

Returns:

  • (String)

    the full name on this address



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

def full_name
  "#{firstname} #{lastname}".strip
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



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

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



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

def require_zipcode?
  true
end

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

Returns true if this fields on this address match the fields on the other address.

Parameters:

Returns:

  • (Boolean)

    true if this fields on this address match the fields on the other address



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

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_textString

Returns a string representation of this state.

Returns:

  • (String)

    a string representation of this state



43
44
45
# File 'app/models/spree/address.rb', line 43

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

#to_sString

Returns the full name on the address followed by the first line of the address.

Returns:

  • (String)

    the full name on the address followed by the first line of the address



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

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