From 5d6d3c7d7cc06db68f329b8a03fb243b145609ac Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Sun, 24 May 2009 12:50:56 -0400
Subject: [PATCH] Add .near class method to including class.

---
 lib/geocoder.rb | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/lib/geocoder.rb b/lib/geocoder.rb
index 7cabb49d..90c4d4e8 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.
-- 
GitLab