Class: Location

Inherits:
Object
  • Object
show all
Includes:
CampfireLogic::Base, Geocoder::Model::Mongoid, Mongoid::Document, Mongoid::Timestamps, Stateflow
Defined in:
app/models/location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CampfireLogic::Base

included

Instance Attribute Details

#geocoder_resultObject

Macros =========================================================================================



40
41
42
# File 'app/models/location.rb', line 40

def geocoder_result
  @geocoder_result
end

Class Method Details

.geocodeObject



84
85
86
87
88
# File 'app/models/location.rb', line 84

def self.geocode
  p "Total not-geocoded locations: #{Location.not_geocoded.count}"
  Location.not_geocoded.each{ |l| l.validate_address_and_localize }
  "Not-geocoded locations remaining: #{Location.not_geocoded.count}"
end

.parse_geo(location, geo) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/location.rb', line 71

def self.parse_geo location, geo
  if Geocoder::Configuration.lookup == :google
    location.geocoder_result = GeocoderGoogleResult.new geo.data
  else
    location.address1 = geo.data['line1']
    location.city = geo.data['city']
    location.state = geo.data['state']
  end

  location.country, location.country_long_name, location[:state], location.state_long_name = geo.country_code, geo.country, geo.state_code, geo.state
  location.coordinates = [geo.longitude, geo.latitude]
end

Instance Method Details

#address1=(value) ⇒ Object

Instance methods: Overrides ====================================================================



91
92
93
94
# File 'app/models/location.rb', line 91

def address1=(value)
  clear_geocoding if self.address1 != value
  self[:address1] = value
end

#call_geocodeObject

Geocodes this location.



125
126
127
128
129
130
131
# File 'app/models/location.rb', line 125

def call_geocode
  return :new if self.geocoding_address.blank?
  geocode unless geocoded?
  return :new unless Geocoder::Configuration.lookup == :google
  audit_address
  self.geocoder_result.try(:partial_match?) ? :partial_match : :validated
end

#city=(value) ⇒ Object



96
97
98
99
# File 'app/models/location.rb', line 96

def city=(value)
  clear_geocoding if self.city != value
  self[:city] = value
end

#city_localeObject

Returns this location’s city locale.



160
161
162
# File 'app/models/location.rb', line 160

def city_locale
  self.locale.parent
end

#fix_duplicate_cityObject



164
165
166
167
168
169
170
171
172
# File 'app/models/location.rb', line 164

def fix_duplicate_city
  duplicate_city = city_locale
  valid_city = Locale.cities.where(:name => duplicate_city.name).first

  if valid_city && valid_city.has_other_locations?
    self.locale.parent = valid_city
    self.locale.save
  end
end

#geocoded?Boolean

Returns true if this location has a latitude and longitude.

Returns:

  • (Boolean)


134
135
136
# File 'app/models/location.rb', line 134

def geocoded?
  self.coordinates.is_a?(Array)
end

#geocoding_addressObject

Returns this location’s address for geocoding. It omits the second address field to simplify post-processing.



113
114
115
# File 'app/models/location.rb', line 113

def geocoding_address
  [address1, city, state].select{|s| ! s.blank?} * ', '
end

#localizeObject

Instance methods: Locales ======================================================================



152
153
154
155
156
157
# File 'app/models/location.rb', line 152

def localize
  return if geocoding_address.blank?
  self.save
  self.locale ? update_locales : create_locales
  self.save
end

#matches_geocoder?(attribute) ⇒ Boolean

Returns true if the specified address field matches the geocoder’s results.

Returns:

  • (Boolean)


139
140
141
# File 'app/models/location.rb', line 139

def matches_geocoder?(attribute)
  geocoder_result.blank? || self.send(attribute) == geocoder_result.send(GOOGLE_MAPS_GEOCODER_FIELDS_BY_LOCATION_FIELD[attribute])
end

#phone=(value) ⇒ Object



101
102
103
# File 'app/models/location.rb', line 101

def phone= value
  self[:phone] = value.gsub /[a-z\.\(\) -]+/,'' if value.present?
end

#state=(value) ⇒ Object



105
106
107
108
# File 'app/models/location.rb', line 105

def state=(value)
  clear_geocoding if self.state != value
  self[:state] = value
end

#state_localeObject

Returns this location’s state locale.



175
176
177
# File 'app/models/location.rb', line 175

def state_locale
  self.city_locale.parent
end

#street_address(delimiter = ' ') ⇒ Object

Returns this location’s street address, combining the two address fields with the specified delimiter. Latter defaults to a space.



118
119
120
# File 'app/models/location.rb', line 118

def street_address( delimiter = ' ' )
  [address1, address2].select{|s| ! s.blank?} * delimiter
end

#validate_address_and_localizeObject



143
144
145
146
147
148
# File 'app/models/location.rb', line 143

def validate_address_and_localize
  if self.geocode
    self.validate_address!
    self.localize
  end
end