Class: Effective::Address
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Address
- Defined in:
- app/models/effective/address.rb
Constant Summary collapse
- POSTAL_CODE_CA =
Matches ‘T5Z 2B1’
/\A[A-Z]{1}\d{1}[A-Z]{1}\ \d{1}[A-Z]{1}\d{1}\z/
- POSTAL_CODE_US =
matches 5 digits plus optional hyphen and 4 digits
/\A\d{5}(-\d{4})?\z/
Instance Attribute Summary collapse
-
#shipping_address_same_as_billing ⇒ Object
Returns the value of attribute shipping_address_same_as_billing.
Instance Method Summary collapse
- #==(other_address) ⇒ Object
-
#blank? ⇒ Boolean
(also: #empty?)
An address may be set with a category but nothing else.
- #country ⇒ Object
- #country=(country_string) ⇒ Object
- #first_name ⇒ Object
- #last_name ⇒ Object
-
#postal_code=(code) ⇒ Object
If the country is Canada or US, enforce the correct postal code/zip code format.
- #postal_code_looks_american? ⇒ Boolean
- #postal_code_looks_canadian? ⇒ Boolean
- #present? ⇒ Boolean
- #shipping_address_same_as_billing? ⇒ Boolean
- #state ⇒ Object (also: #province)
- #state=(state_string) ⇒ Object (also: #province=)
- #to_html ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#shipping_address_same_as_billing ⇒ Object
Returns the value of attribute shipping_address_same_as_billing.
7 8 9 |
# File 'app/models/effective/address.rb', line 7 def shipping_address_same_as_billing @shipping_address_same_as_billing end |
Instance Method Details
#==(other_address) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'app/models/effective/address.rb', line 114 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', 'addressable_type', 'addressable_id', 'category') } self_attrs == other_attrs end |
#blank? ⇒ Boolean Also known as: empty?
An address may be set with a category but nothing else
124 125 126 |
# File 'app/models/effective/address.rb', line 124 def blank? address1.blank? && address2.blank? && city.blank? && postal_code.blank? end |
#country ⇒ Object
59 60 61 |
# File 'app/models/effective/address.rb', line 59 def country Carmen::Country.coded(country_code).name rescue '' end |
#country=(country_string) ⇒ Object
68 69 70 71 |
# File 'app/models/effective/address.rb', line 68 def country=(country_string) value = Carmen::Country.named(country_string) || Carmen::Country.coded(country_string.try(:upcase)) self.country_code = value.code if value.present? end |
#first_name ⇒ Object
51 52 53 |
# File 'app/models/effective/address.rb', line 51 def first_name full_name.split(' ').first rescue full_name end |
#last_name ⇒ Object
55 56 57 |
# File 'app/models/effective/address.rb', line 55 def last_name full_name.gsub(first_name, '').strip rescue full_name end |
#postal_code=(code) ⇒ Object
If the country is Canada or US, enforce the correct postal code/zip code format
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/effective/address.rb', line 92 def postal_code=(code) if code.presence.kind_of?(String) if country_code == 'CA' code = code.upcase.gsub(/[^A-Z0-9]/, '') code = code.insert(3, ' ') if code.length == 6 elsif country_code == 'US' code = code.gsub(/[^0-9]/, '') code = code.insert(5, '-') if code.length == 9 end end super(code) end |
#postal_code_looks_american? ⇒ Boolean
110 111 112 |
# File 'app/models/effective/address.rb', line 110 def postal_code_looks_american? postal_code.to_s.match(POSTAL_CODE_US).present? end |
#postal_code_looks_canadian? ⇒ Boolean
106 107 108 |
# File 'app/models/effective/address.rb', line 106 def postal_code_looks_canadian? postal_code.to_s.match(POSTAL_CODE_CA).present? end |
#present? ⇒ Boolean
129 130 131 |
# File 'app/models/effective/address.rb', line 129 def present? !blank? end |
#shipping_address_same_as_billing? ⇒ Boolean
149 150 151 |
# File 'app/models/effective/address.rb', line 149 def shipping_address_same_as_billing? EffectiveResources.truthy?(shipping_address_same_as_billing) end |
#state ⇒ Object Also known as: province
63 64 65 |
# File 'app/models/effective/address.rb', line 63 def state Carmen::Country.coded(country_code).subregions.coded(state_code).name rescue '' end |
#state=(state_string) ⇒ Object Also known as: province=
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/effective/address.rb', line 73 def state=(state_string) if country.present? subregions = (Carmen::Country.coded(country_code).subregions rescue nil) if subregions.present? value = subregions.named(state_string) || subregions.coded(state_string.try(:upcase)) else value = nil end self.state_code = value.code if value.present? else Rails.logger.info 'No country set. Try calling country= before state=' puts 'No country set. Try calling country= before state=' end end |
#to_html ⇒ Object
145 146 147 |
# File 'app/models/effective/address.rb', line 145 def to_html to_s.gsub(/\n/, "<br>\n").gsub(' ', ' ').html_safe end |
#to_s ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/models/effective/address.rb', line 133 def to_s output = '' output += "#{full_name}\n" if full_name.present? output += "#{address1}\n" if address1.present? output += "#{address2}\n" if address2.present? output += "#{address3}\n" if respond_to?(:address3) && address3.present? output += [city.presence, state_code.presence, " #{postal_code.presence}"].compact.join(' ') output += "\n" output += country.to_s output end |