Module: CollectiveIdea::Acts::Geocodable::InstanceMethods

Defined in:
lib/acts_as_geocodable.rb

Instance Method Summary collapse

Instance Method Details

#attach_geocode_nowObject



259
260
261
# File 'lib/acts_as_geocodable.rb', line 259

def attach_geocode_now
  attach_geocode
end

#attach_geocode_with_delayObject



263
264
265
# File 'lib/acts_as_geocodable.rb', line 263

def attach_geocode_with_delay
  self.send_later :attach_geocode
end

#distance_to(destination, options = {}) ⇒ Object

Get the distance to the given destination. The destination can be an acts_as_geocodable model, a Geocode, or a string

myhome.distance_to "Chicago, IL"
myhome.distance_to "49423"
myhome.distance_to other_model

Options

  • :units: :miles or :kilometers

  • :formula: The formula to use to calculate the distance. This can be any formula supported by Graticule. The default is :haversine.



251
252
253
254
255
256
257
# File 'lib/acts_as_geocodable.rb', line 251

def distance_to(destination, options = {})
  units = options[:units] || acts_as_geocodable_options[:units]
  formula = options[:formula] || :haversine
  
  geocode = self.class.location_to_geocode(destination)
  self.geocode.distance_to(geocode, units, formula)
end

#geocodeObject

Get the geocode for this model



226
227
228
# File 'lib/acts_as_geocodable.rb', line 226

def geocode
  geocoding.geocode if geocoding
end

#to_locationObject

Create a Graticule::Location



231
232
233
234
235
236
237
# File 'lib/acts_as_geocodable.rb', line 231

def to_location
  returning Graticule::Location.new do |location|
    [:street, :locality, :region, :postal_code, :country].each do |attr|
      location.send "#{attr}=", geo_attribute(attr)
    end
  end
end