Class: Spree::Address

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultObject



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

def self.default
  country = Spree::Country.find(Spree::Config[:default_country_id]) rescue Spree::Country.first
  new({ country: country }, without_protection: true)
end

Instance Method Details

#==(other_address) ⇒ Object



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

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_hashObject

Generates an ActiveMerchant compatible address hash



76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/spree/address.rb', line 76

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



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

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

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/spree/address.rb', line 71

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

#full_nameObject

Can modify an address if it’s not been used in an order (but checkouts controller has finer control) def editable?

new_record? || (shipments.empty? && checkouts.empty?)

end



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

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

#phone_validateObject

Disconnected since there’s no code to display error messages yet OR matching client-side validation



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

def phone_validate
  return if phone.blank?
  n_digits = phone.scan(/[0-9]/).size
  valid_chars = (phone =~ /^[-+()\/\s\d]+$/)
  errors.add :phone, :invalid unless (n_digits > 5 && valid_chars)
end

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

Returns:

  • (Boolean)


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_textObject



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

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

#to_sObject



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

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