Module: ActiveRecord::Base::NearestNeighbor::CloseTo

Included in:
ClassMethods
Defined in:
lib/active_record_nearest_neighbor/close_to.rb

Instance Method Summary collapse

Instance Method Details

#close_to(longitude_or_object, longitude_or_latitude_or_options = {}, options = {}) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_record_nearest_neighbor/close_to.rb', line 3

def close_to(longitude_or_object, longitude_or_latitude_or_options={}, options={})
  scope_method = :bounding_box
  scope_params = {}

  if longitude_or_object.class.ancestors.include?(ActiveRecord::Base)  
    # We are using an object as the point reference
    object = longitude_or_object
    options = longitude_or_latitude_or_options

    scope_method = options[:method] || scope_method
    scope_params = {id: options[:id], distance: options[:distance], limit: options[:limit]}.
    merge(longitude: object.longitude, latitude: object.latitude, id: object.id)
  else
    # We are using longitude and latitude
    longitude = longitude_or_object
    latitude = longitude_or_latitude_or_options

    scope_method = options[:method] || scope_method

    scope_params = {id: options[:id], distance: options[:distance], limit: options[:limit]}.
    merge(longitude: longitude, latitude: latitude)
  end

  close_to_with_scope(scope_method, scope_params)
end