Module: Geokit::ActsAsMappable

Extended by:
ActiveSupport::Concern
Defined in:
lib/geokit-rails3/acts_as_mappable.old.rb,
lib/geokit-rails3/acts_as_mappable.rb

Overview

Contains the class method acts_as_mappable targeted to be mixed into ActiveRecord. When mixed in, augments find services such that they provide distance calculation query services. The find method accepts additional options:

  • :origin - can be

    1. a two-element array of latititude/longitude – :origin=>

    2. a geocodeable string – :origin=>‘100 Spear st, San Francisco, CA’

    3. an object which responds to lat and lng methods, or latitude and longitude methods, or whatever methods you have specified for lng_column_name and lat_column_name

Other finder methods are provided for specific queries. These are:

  • find_within (alias: find_inside)

  • find_beyond (alias: find_outside)

  • find_closest (alias: find_nearest)

  • find_farthest

Counter methods are available and work similarly to finders.

If raw SQL is desired, the distance_sql method can be used to obtain SQL appropriate to use in a find_by_sql call.

Defined Under Namespace

Modules: ClassMethods, Glue Classes: UnsupportedAdapter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.end_of_reflection_chain(through, klass) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 294

def self.end_of_reflection_chain(through, klass)
  while through
    reflection = nil
    if through.is_a?(Hash)
      association, through = through.to_a.first
    else
      association, through = through, nil
    end

    if reflection = klass.reflect_on_association(association)
      klass = reflection.klass
    else
      raise ArgumentError, "You gave #{association} in :through, but I could not find it on #{klass}."
    end
  end

  reflection
end

Instance Method Details

#auto_geocode_addressObject

this is the callback for auto_geocoding



280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 280

def auto_geocode_address
  address=self.send(auto_geocode_field).to_s
  geo=Geokit::Geocoders::MultiGeocoder.geocode(address)

  if geo.success
    self.send("#{lat_column_name}=", geo.lat)
    self.send("#{lng_column_name}=", geo.lng)
  else
    errors.add(auto_geocode_field, auto_geocode_error_message)
  end

  geo.success
end