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



21
22
23
24
# File 'app/models/spree/address.rb', line 21

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



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

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



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

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



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

#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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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