diff --git a/lib/geocoder.rb b/lib/geocoder.rb index 7cabb49da1c9d8635916cabe83a590e06476052e..90c4d4e8370d3e4baea0de79219bdefd60267458 100644 --- a/lib/geocoder.rb +++ b/lib/geocoder.rb @@ -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.