Module: StrictGeocoder::Store::ActiveRecord

Includes:
Geocoder::Store::Base
Defined in:
lib/strict_geocoder/stores/active_record.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Implementation of ‘included’ hook method.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/strict_geocoder/stores/active_record.rb', line 16

def self.included(base)
  base.class_eval do

    ##
    # 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::Store::ActiveRecord::ClassMethods.near_scope_options
    # for details).
    #
    scope :near!, lambda{ |location, *args|
      point = Geocoder::Calculations.extract_coordinates(location)
      raise StrictGeocoder::InvalidLocation, "The provided location didn't return as valid" \
        and return if point == [
          Geocoder::Calculations::NAN, Geocoder::Calculations::NAN
        ]

      options = near_scope_options(*point, *args)
      select(options[:select]).where(options[:conditions]).
        order(options[:order])
    }
  end
end