Class: Address

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultObject



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

def self.default
  new :country => Country.find(Spree::Config[:default_country_id])
end

Instance Method Details

#==(other_address) ⇒ Object



70
71
72
73
74
75
76
77
# File 'app/models/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

#cloneObject



66
67
68
# File 'app/models/address.rb', line 66

def clone
  Address.new(self.attributes.except("id", "updated_at", "created_at"))
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  attributes.except("id", "created_at", "updated_at", "order_id", "country_id").all? {|k,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/address.rb', line 39

def full_name
  self.firstname + " " + self.lastname
end

#phone_validateObject

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



13
14
15
16
17
18
19
20
# File 'app/models/address.rb', line 13

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

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

Returns:

  • (Boolean)


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

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_name_validateObject



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

def state_name_validate
  country = country_id ? Country.find(country_id) : nil
  return if country.blank? || country.states.empty?
  if state_name.blank? || country.states.where(["name = ? OR abbr = ?", state_name, state_name]).empty?
    errors.add(:state, :invalid)
  end
end

#state_textObject



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

def state_text
  state.nil? ? state_name : (state.abbr.blank? ? state.name : state.abbr)
end

#to_sObject



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

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

#zoneObject



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

def zone
  (state && state.zone) ||
  (country && country.zone)
end

#zonesObject



52
53
54
# File 'app/models/address.rb', line 52

def zones
  Zone.match(self)
end