From 253af9ded26ddd5857a49c50bc008bdb3d7e78b6 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Fri, 4 Mar 2011 17:34:05 -0500
Subject: [PATCH] Allow passing a block to 'geocoded_by' in model.

For advanced/custom handling of gecoding result data.
---
 lib/geocoder/active_record.rb |  4 +++-
 lib/geocoder/railtie.rb       |  5 +++--
 test/geocoder_test.rb         | 14 +++++++++++---
 test/test_helper.rb           |  6 ++++++
 4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/lib/geocoder/active_record.rb b/lib/geocoder/active_record.rb
index fdb38c73..018140d5 100644
--- a/lib/geocoder/active_record.rb
+++ b/lib/geocoder/active_record.rb
@@ -201,8 +201,10 @@ module Geocoder
         end
         args = [send(address_method)]
       end
+      # passing a block to this method overrides the one given in the model
+      b = block_given?? block : self.class.geocoder_options[:block]
       if result = Geocoder.search(*args).first
-        block.call(result)
+        b.call(result)
       end
     end
 
diff --git a/lib/geocoder/railtie.rb b/lib/geocoder/railtie.rb
index 20bad241..5c229688 100644
--- a/lib/geocoder/railtie.rb
+++ b/lib/geocoder/railtie.rb
@@ -28,11 +28,12 @@ module Geocoder
         ##
         # Set attribute names and include the Geocoder module.
         #
-        def self.geocoded_by(address_attr, options = {})
+        def self.geocoded_by(address_attr, options = {}, &block)
           _geocoder_init(
             :user_address => address_attr,
             :latitude  => options[:latitude]  || :latitude,
-            :longitude => options[:longitude] || :longitude
+            :longitude => options[:longitude] || :longitude,
+            :block => block
           )
         end
 
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index 61dfb313..a0a9ab67 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -6,10 +6,18 @@ class GeocoderTest < Test::Unit::TestCase
     Geocoder::Configuration.lookup = :google
   end
 
-  def test_fetch_coordinates
+  def test_fetch_coordinates_assigns_and_returns_coordinates
     v = Venue.new(*venue_params(:msg))
-    assert_equal [40.750354, -73.993371], v.fetch_coordinates
-    assert_equal [40.750354, -73.993371], [v.latitude, v.longitude]
+    coords = [40.750354, -73.993371]
+    assert_equal coords, v.fetch_coordinates
+    assert_equal coords, [v.latitude, v.longitude]
+  end
+
+  def test_fetch_address_assigns_and_returns_address
+    v = Landmark.new(*landmark_params(:msg))
+    address = "4 Penn Plaza, New York, NY 10001, USA"
+    assert_equal address, v.fetch_address
+    assert_equal address, v.address
   end
 
   # sanity check
diff --git a/test/test_helper.rb b/test/test_helper.rb
index b2017a9d..f621cea7 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -110,4 +110,10 @@ class Test::Unit::TestCase
       :msg => ["Madison Square Garden", "4 Penn Plaza, New York, NY"]
     }[abbrev]
   end
+
+  def landmark_params(abbrev)
+    {
+      :msg => ["Madison Square Garden", 40.750354, -73.993371]
+    }[abbrev]
+  end
 end
-- 
GitLab