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.
6 7 8 |
# File 'app/models/effective/address.rb', line 6 def shipping_address_same_as_billing @shipping_address_same_as_billing end |
Instance Method Details
#==(other_address) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'app/models/effective/address.rb', line 113 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
123 124 125 |
# File 'app/models/effective/address.rb', line 123 def blank? address1.blank? && address2.blank? && city.blank? && postal_code.blank? end |
#country ⇒ Object
58 59 60 |
# File 'app/models/effective/address.rb', line 58 def country Carmen::Country.coded(country_code).name rescue '' end |
#country=(country_string) ⇒ Object
67 68 69 70 |
# File 'app/models/effective/address.rb', line 67 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
50 51 52 |
# File 'app/models/effective/address.rb', line 50 def first_name full_name.split(' ').first rescue full_name end |
#last_name ⇒ Object
54 55 56 |
# File 'app/models/effective/address.rb', line 54 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
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/effective/address.rb', line 91 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
109 110 111 |
# File 'app/models/effective/address.rb', line 109 def postal_code_looks_american? postal_code.to_s.match(POSTAL_CODE_US).present? end |
#postal_code_looks_canadian? ⇒ Boolean
105 106 107 |
# File 'app/models/effective/address.rb', line 105 def postal_code_looks_canadian? postal_code.to_s.match(POSTAL_CODE_CA).present? end |
#present? ⇒ Boolean
128 129 130 |
# File 'app/models/effective/address.rb', line 128 def present? !blank? end |
#shipping_address_same_as_billing? ⇒ Boolean
148 149 150 151 152 153 154 |
# File 'app/models/effective/address.rb', line 148 def shipping_address_same_as_billing? if defined?(::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES) # Rails 5 ::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(shipping_address_same_as_billing) else ::ActiveRecord::Type::Boolean.new.cast(shipping_address_same_as_billing) end end |
#state ⇒ Object Also known as: province
62 63 64 |
# File 'app/models/effective/address.rb', line 62 def state Carmen::Country.coded(country_code).subregions.coded(state_code).name rescue '' end |
#state=(state_string) ⇒ Object Also known as: province=
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/models/effective/address.rb', line 72 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
144 145 146 |
# File 'app/models/effective/address.rb', line 144 def to_html to_s.gsub(/\n/, "<br>\n").gsub(' ', ' ').html_safe end |
#to_s ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/models/effective/address.rb', line 132 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 |