Module: Geocoder::Orm::ActiveRecord
- Defined in:
- lib/geocoder/orms/active_record.rb,
lib/geocoder/orms/active_record_legacy.rb
Defined Under Namespace
Modules: ClassMethods, Legacy
Class Method Summary collapse
-
.included(base) ⇒ Object
Implementation of ‘included’ hook method.
Instance Method Summary collapse
-
#geocode ⇒ Object
Look up coordinates and assign to
latitude
andlongitude
attributes (or other as specified ingeocoded_by
). -
#reverse_geocode ⇒ Object
Look up address and assign to
address
attribute (or other as specified inreverse_geocoded_by
).
Methods included from Legacy
#fetch_address, #fetch_address!, #fetch_coordinates, #fetch_coordinates!
Methods included from Base
#distance_to, #geocoded?, #nearbys, #to_coordinates
Class Method Details
.included(base) ⇒ Object
Implementation of ‘included’ hook method.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/geocoder/orms/active_record.rb', line 15 def self.included(base) base.extend ClassMethods base.class_eval do # scope: geocoded objects scope :geocoded, lambda { {:conditions => "#{[:latitude]} IS NOT NULL " + "AND #{[:longitude]} IS NOT NULL"}} # scope: not-geocoded objects scope :not_geocoded, lambda { {:conditions => "#{[:latitude]} IS NULL " + "OR #{[:longitude]} IS NULL"}} ## # Find all objects within a radius of the given location. # Location may be either a string to geocode or an array of # coordinates (<tt>[lat,lon]</tt>). Also takes an options hash # (see Geocoder::Orm::ActiveRecord::ClassMethods.near_scope_options # for details). # scope :near, lambda{ |location, *args| latitude, longitude = location.is_a?(Array) ? location : Geocoder.coordinates(location) if latitude and longitude (latitude, longitude, *args) else {} end } end end |
Instance Method Details
#geocode ⇒ Object
Look up coordinates and assign to latitude
and longitude
attributes (or other as specified in geocoded_by
). Returns coordinates (array).
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/geocoder/orms/active_record.rb', line 197 def geocode do_lookup(false) do |o,rs| r = rs.first unless r.latitude.nil? or r.longitude.nil? o.send :write_attribute, self.class.[:latitude], r.latitude o.send :write_attribute, self.class.[:longitude], r.longitude end r.coordinates end end |
#reverse_geocode ⇒ Object
Look up address and assign to address
attribute (or other as specified in reverse_geocoded_by
). Returns address (string).
214 215 216 217 218 219 220 221 222 |
# File 'lib/geocoder/orms/active_record.rb', line 214 def reverse_geocode do_lookup(true) do |o,rs| r = rs.first unless r.address.nil? o.send :write_attribute, self.class.[:fetched_address], r.address end r.address end end |