Skip to content
Snippets Groups Projects
Commit 5d6d3c7d authored by Alex Reisner's avatar Alex Reisner
Browse files

Add .near class method to including class.

parent 315120ff
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ module Geocoder
# Implementation of 'included' hook method.
#
def self.included(base)
base.extend ClassMethods
base.class_eval do
# named scope: geocoded objects
......@@ -43,6 +44,26 @@ module Geocoder
coords.split(',')[0...2].reverse.map{ |i| i.to_f }
end
##
# Methods which will be class methods of the including class.
#
module ClassMethods
##
# Find all ads within a radius (in miles) of the given location (string).
#
def near(location, radius = 100, options = {})
latitude, longitude = Geocoder.fetch_coordinates(location)
return [] unless (latitude and longitude)
# don't pass :table_name option to nearby_mysql_query
table_name = options[:table_name] || self.to_s.tableize
options.delete :table_name
query = Geocoder.nearby_mysql_query(table_name,
latitude, longitude, radius.to_i, options)
find_by_sql(query)
end
end
##
# Calculate the distance from the object to a point (lat,lon). Valid units
# are defined in <tt>distance_between</tt> class method.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment