Class: Effective::Address

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/address.rb

Instance Method Summary collapse

Instance Method Details

#==(other_address) ⇒ Object



66
67
68
69
70
71
72
73
# File 'app/models/effective/address.rb', line 66

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', 'address2') }

  self_attrs == other_attrs
end

#countryObject



35
36
37
# File 'app/models/effective/address.rb', line 35

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

#country=(country_string) ⇒ Object



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

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

#empty?Boolean

An address may be set with a category but nothing else This is considered empty

Returns:

  • (Boolean)


77
78
79
# File 'app/models/effective/address.rb', line 77

def empty?
  !full_name.present? and !address1.present? and !address2.present? and !city.present? and !state_code.present? and !country_code.present? and !postal_code.present?
end

#first_nameObject



27
28
29
# File 'app/models/effective/address.rb', line 27

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

#last_nameObject



31
32
33
# File 'app/models/effective/address.rb', line 31

def last_name
  full_name.gsub(first_name, '').strip rescue full_name
end

#postal_code_looks_canadian?Boolean

Returns:

  • (Boolean)


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

def postal_code_looks_canadian?
  postal_code.gsub(/ /, '').strip.match(/^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/).present? rescue false # Matches T5T2T1 or T5T-2T1
end

#provinceObject



43
44
45
# File 'app/models/effective/address.rb', line 43

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

#stateObject



39
40
41
# File 'app/models/effective/address.rb', line 39

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

#state=(state_string) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'app/models/effective/address.rb', line 52

def state=(state_string)
  if country.present?
    value = Carmen::Country.coded(country_code).subregions.named(state_string) || Carmen::Country.coded(country_code).subregions.coded(state_string.try(:upcase))
    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



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

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

#to_sObject



81
82
83
84
85
86
87
# File 'app/models/effective/address.rb', line 81

def to_s
  output = "#{full_name}\n"
  output += "#{address1}\n"
  output += "#{address2}\n" if address2.present?
  output += "#{city}, #{state}\n"
  output += "#{country}, #{postal_code}"
end