Class: Address
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Address
- Defined in:
- app/models/address.rb
Class Method Summary collapse
Instance Method Summary collapse
- #==(other_address) ⇒ Object
- #clone ⇒ Object
- #empty? ⇒ Boolean
-
#full_name ⇒ Object
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.
-
#phone_validate ⇒ Object
disconnected since there’s no code to display error messages yet OR matching client-side validation.
- #same_as?(other) ⇒ Boolean (also: #same_as)
- #state_name_validate ⇒ Object
- #state_text ⇒ Object
- #to_s ⇒ Object
- #zone ⇒ Object
- #zones ⇒ Object
Class Method Details
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 |
#clone ⇒ Object
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
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_name ⇒ Object
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_validate ⇒ Object
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
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_validate ⇒ Object
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_text ⇒ Object
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_s ⇒ Object
62 63 64 |
# File 'app/models/address.rb', line 62 def to_s "#{full_name}: #{address1}" end |
#zone ⇒ Object
47 48 49 50 |
# File 'app/models/address.rb', line 47 def zone (state && state.zone) || (country && country.zone) end |