From d6e1979c4e6261d5cf8eb5fe183ae2f4beddb2ba Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Fri, 23 Oct 2009 19:30:14 -0400
Subject: [PATCH] Add 'near' named scope and deprecate find_near class method.

---
 lib/geocoder.rb | 40 ++++++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 14 deletions(-)

diff --git a/lib/geocoder.rb b/lib/geocoder.rb
index 04763288..38573af3 100644
--- a/lib/geocoder.rb
+++ b/lib/geocoder.rb
@@ -11,15 +11,30 @@ module Geocoder
     base.class_eval do
 
       # named scope: geocoded objects
-	    named_scope :geocoded,
-	      :conditions => "#{geocoder_options[:latitude]} IS NOT NULL " +
-	        "AND #{geocoder_options[:longitude]} IS NOT NULL"
+      named_scope :geocoded,
+        :conditions => "#{geocoder_options[:latitude]} IS NOT NULL " +
+          "AND #{geocoder_options[:longitude]} IS NOT NULL"
 
       # named scope: not-geocoded objects
-	    named_scope :not_geocoded,
-	      :conditions => "#{geocoder_options[:latitude]} IS NULL " +
-	        "OR #{geocoder_options[:longitude]} IS NULL"
-	  end
+      named_scope :not_geocoded,
+        :conditions => "#{geocoder_options[:latitude]} IS NULL " +
+          "OR #{geocoder_options[:longitude]} IS NULL"
+      
+      ##
+      # Find all objects within a radius (in miles) of the given location
+      # (address string). Location (the first argument) may be either a string
+      # to geocode or an array of coordinates (<tt>[lat,long]</tt>).
+      #
+      named_scope :near, lambda{ |location, *args|
+        latitude, longitude = location.is_a?(Array) ?
+          location : Geocoder.fetch_coordinates(location)
+        if latitude and longitude
+          find_near_options(latitude, longitude, *args)
+        else
+          {}
+        end
+      }
+    end
   end
     
   ##
@@ -28,15 +43,12 @@ module Geocoder
   module ClassMethods
 
     ##
-    # Find all objects within a radius (in miles) of the given location
-    # (address string). Location (the first argument) may be either a string
-    # to geocode or an array of coordinates (<tt>[lat,long]</tt>).
+    # DEPRECATED: Please use the +near+ method/named scope instead.
     #
     def find_near(location, radius = 20, options = {})
-      latitude, longitude = location.is_a?(Array) ?
-        location : Geocoder.fetch_coordinates(location)
-      return [] unless (latitude and longitude)
-      all(find_near_options(latitude, longitude, radius, options))
+      warn "Geocoder deprecation warning: the 'find_near' class method is " +
+        "deprecated, please use the 'near' method, which is a named scope."
+      near(location, radius, options)
     end
     
     ##
-- 
GitLab