Class: Effective::Address

Inherits:
ActiveRecord::Base
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#shipping_address_same_as_billingObject

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



111
112
113
114
115
116
117
118
# File 'app/models/effective/address.rb', line 111

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

Returns:

  • (Boolean)


121
122
123
# File 'app/models/effective/address.rb', line 121

def blank?
  address1.blank? && address2.blank? && city.blank? && postal_code.blank?
end

#countryObject



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

def country
  Carmen::Country.coded(country_code).name rescue ''
end

#country=(country_string) ⇒ Object



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

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_nameObject



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

def first_name
  full_name.split(' ').first rescue full_name
end

#last_nameObject



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

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



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/effective/address.rb', line 89

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

Returns:

  • (Boolean)


107
108
109
# File 'app/models/effective/address.rb', line 107

def postal_code_looks_american?
  postal_code.to_s.match(POSTAL_CODE_US).present?
end

#postal_code_looks_canadian?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/effective/address.rb', line 103

def postal_code_looks_canadian?
  postal_code.to_s.match(POSTAL_CODE_CA).present?
end

#present?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'app/models/effective/address.rb', line 126

def present?
  !blank?
end

#shipping_address_same_as_billing?Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
# File 'app/models/effective/address.rb', line 146

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

#stateObject Also known as: province



60
61
62
# File 'app/models/effective/address.rb', line 60

def state
  Carmen::Country.coded(country_code).subregions.coded(state_code).name rescue ''
end

#state=(state_string) ⇒ Object Also known as: province=



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/effective/address.rb', line 70

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_htmlObject



142
143
144
# File 'app/models/effective/address.rb', line 142

def to_html
  to_s.gsub(/\n/, "<br>\n").gsub('  ', '&nbsp;&nbsp;').html_safe
end

#to_sObject



130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/effective/address.rb', line 130

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