Class: Address

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Labelize
Defined in:
app/models/address.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_address(string) ⇒ Object



86
87
88
89
# File 'app/models/address.rb', line 86

def self.find_by_address(string)
  template = self.parse(string)
  self.find_by(template.attributes.symbolize_keys!.slice(:line1, :line2, :city, :state))
end

.find_or_create_by_example(other) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/models/address.rb', line 142

def self.find_or_create_by_example(other)
  res = self
  res = res.where(:line1 => other.line1) if other.line1
  res = res.where(:line2 => other.line2) if other.line2
  res = res.where(:city => other.city) if other.city
  res = res.where(:state => other.state) if other.state
  res = res.where(:country => other.country) if other.country
  res = res.where(:postal => other.postal) if other.postal

  a = nil
  addresses = res
  if addresses != self
    a = Address.create(other.attributes.symbolize_keys!.except(:id, :created_at, :updated_at)) if addresses == []
    a ||= addresses[0]
  end

  a
end

.new_from_hash(incoming_hash) ⇒ Object



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

def self.new_from_hash(incoming_hash)
  p = {}
  incoming_hash.each {|key, val| p[key] = val if not val.blank?}
  a = self.new
  a.line1 = p[:line1] || p[:address]
  a.line2 = p[:line2]
  a.city = p[:city]
  a.state = p[:state]
  a.country = p[:country]
  a.postal = p[:postal] || p[:zipcode] || p[:zip]
  a.label = Label.get("Work")
  a
end

.parse(string) ⇒ Object



122
123
124
125
126
# File 'app/models/address.rb', line 122

def self.parse(string)
  a = self.new
  a.address = string
  a
end

Instance Method Details

#addressObject

require ‘carmen’

require 'geocoder'
geocoded_by :oneline, latitude: :lat, longitude: :lon
after_validation :geocode


12
13
14
# File 'app/models/address.rb', line 12

def address
  oneline
end

#address=(string) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/address.rb', line 51

def address=(string)
  return unless not string.blank? and not postal_changed?
  return unless string != line1
  return if (string == oneline() || string == oneline({full: true}))

  have_geocoder = defined?(Geocoder)
  r = Geocoder.search(string) rescue nil
  @already_geocoded = true
  
  if not r or r.length == 0
    self.errors.add(:address, :did_not_geocode, message: "No locations found") if have_geocoder
    self.line1 = string
  else
    res = r[0]
    self.update_lat_lon(res)
    if res.respond_to?(:street)
      self.line1 = (res.house_number ? res.house_number + " " : "") + res.street
      self.line2 = nil
      c = Carmen::Country.coded(res.country_code) rescue nil
      s = c.subregions.named(res.state) if c
      self.state = s.code.to_s if s
    else
      self.line1 = res.street_address.to_s rescue string
      self.line2 = nil
      self.state = res.state_code.to_s
    end
      self.city = res.city.to_s
      self.postal = res.postal_code.to_s
      self.country = res.country_code.to_s.upcase
  end

  self.errors.add(:address, :too_many_matches, message: "Too many matches") if have_geocoder && r && r.length > 1 
  self
end

#admin_object_nameObject



161
162
163
# File 'app/models/address.rb', line 161

def admin_object_name
  [line1, city, postal].join(" ").strip rescue ""
end

#as_json(options = {}) ⇒ Object



91
92
93
94
95
96
97
# File 'app/models/address.rb', line 91

def as_json(options={})
  for_user = options.delete(:for_user)
  res = super(options)
  res[:address] = oneline
  res[:label] = self.label.value if self.label
  res
end

#assign_attributes(hash) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/models/address.rb', line 22

def assign_attributes(hash)
  if hash.has_key?('address')
    a = hash['address']
    hash.delete('address')
  end
  super(hash)
  self.address = a if a
  nil
end

#label=(text_or_label) ⇒ Object



32
33
34
35
36
# File 'app/models/address.rb', line 32

def label=(text_or_label)
  l   = Label.get(text_or_label.to_s) if text_or_label.is_a?(String) or text_or_label.is_a?(Symbol)
  l ||= text_or_label
  write_attribute(:label_id, l.id)
end

#oneline(options = {}) ⇒ Object



114
115
116
117
118
119
120
# File 'app/models/address.rb', line 114

def oneline(options={})
  if options[:full]
    return [line1, line2, city, state, postal].compact.join(", ").gsub(/, ,/, ",")
  else
    return "#{line1}, #{city}, #{state}"
  end
end

#personObject



165
166
167
# File 'app/models/address.rb', line 165

def person
  self.people.first rescue nil
end

#save(options = {}) ⇒ Object



16
17
18
19
20
# File 'app/models/address.rb', line 16

def save(options={})
  r = super(options)
  @already_geocoded = false
  r
end

#update_from_postalObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/address.rb', line 99

def update_from_postal
  return if @already_geocoded or self.postal.blank?
  r = Geocoder.search(self.postal) rescue nil
  if not r or r.length == 0
    return
    self.errors.add(:postal, :cannot_locate_postal_code, message: "Couldn't locate postal code")
  else
    res = r[0]
    self.update_lat_lon(res)
    self.city = res.city
    self.state = res.state_code
    self.country = res.country_code
  end
end

#update_lat_lon(info = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/address.rb', line 38

def update_lat_lon(info=nil)
  info ||= Geocoder.search(self.oneline).first rescue nil
  if info
    if info.respond_to?(:latitude)
      self.lat = info.latitude
      self.lon = info.longitude
    else
      self.lat = info.geometry["location"]["lat"] rescue nil
      self.lon = info.geometry["location"]["lng"] rescue nil
    end
  end
end