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



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

def self.default
  country = Spree::Country.find_by_id(Spree::Config[:default_country_id])
  new :country_id => country.try(:id) || Country.first.id
end

Instance Method Details

#==(other_address) ⇒ Object



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

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



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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)


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

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



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

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

#state_validateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/spree/address.rb', line 21

def state_validate
  #skip state validation without country (also required)
  #or when disabled by perfernce
  return if self.country_id.blank? || !Spree::Config[:address_requires_state]

  #ensure associated state belongs to country
  if self.state_id.present?
    if self.state.country_id == self.country_id
      self.state_name = nil #not required as we have a valid state and country combo
    else
      if self.state_name.present?

        self.state_id = nil
      else
        errors.add(:state, :invalid)
      end
    end
  end

  #ensure state_name belongs to country without states, or that it matches a predefined state name/abbr
  if self.state_name.present?
    if country.states.present?
      states = country.states.find_all_by_name_or_abbr(self.state_name)

      if states.size == 1
        self.state = states.first
        self.state_name = nil
      else
        errors.add(:state, :invalid)
      end
    end
  end

  #ensure at least one state field is populated
  if self.state_id.blank? && self.state_name.blank?
    errors.add(:state, :blank)
  end

end

#to_sObject



95
96
97
# File 'app/models/spree/address.rb', line 95

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

#zoneObject



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

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

#zonesObject



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

def zones
  Zone.match(self)
end