From de3131d8a371bc17650ff66847cdfca4c267fadb Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Sat, 5 Mar 2011 16:39:38 -0500
Subject: [PATCH] Allow passing block to reverse_geocoded_by.

---
 lib/geocoder/railtie.rb |  5 ++--
 test/geocoder_test.rb   | 13 ++++++++++
 test/test_helper.rb     | 54 ++++++++++++++++++++++++++++++++---------
 3 files changed, 58 insertions(+), 14 deletions(-)

diff --git a/lib/geocoder/railtie.rb b/lib/geocoder/railtie.rb
index 4a717eda..40a683f9 100644
--- a/lib/geocoder/railtie.rb
+++ b/lib/geocoder/railtie.rb
@@ -42,11 +42,12 @@ module Geocoder
     ##
     # Set attribute names and include the Geocoder module.
     #
-    def reverse_geocoded_by(latitude_attr, longitude_attr, options = {})
+    def reverse_geocoded_by(latitude_attr, longitude_attr, options = {}, &block)
       geocoder_init(
         :fetched_address => options[:address] || :address,
         :latitude  => latitude_attr,
-        :longitude => longitude_attr
+        :longitude => longitude_attr,
+        :block => block
       )
     end
 
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index e13e85cc..0915e94e 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -44,6 +44,19 @@ class GeocoderTest < Test::Unit::TestCase
     assert_equal address, v.address
   end
 
+  def test_geocode_fetches_and_assigns_custom_coordinates
+    e = Event.new(*venue_params(:msg))
+    coords = [40.750354, -73.993371]
+    e.geocode
+    assert_equal coords.map{ |c| c.to_s }.join(','), e.coordinates
+  end
+
+  def test_geocode_fetches_and_assigns_custom_address_components
+    e = Party.new(*landmark_params(:msg))
+    e.geocode
+    assert_equal "US", e.country
+  end
+
 
   # --- Google ---
 
diff --git a/test/test_helper.rb b/test/test_helper.rb
index ae602b61..569b8b28 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -27,6 +27,14 @@ module ActiveRecord
     end
 
     def self.scope(*args); end
+
+    def method_missing(name, *args, &block)
+      if name.to_s[-1..-1] == "="
+        write_attribute name.to_s[0...-1], *args
+      else
+        read_attribute name
+      end
+    end
   end
 end
 
@@ -73,13 +81,6 @@ class Venue < ActiveRecord::Base
     write_attribute :name, name
     write_attribute :address, address
   end
-
-  ##
-  # If method not found, assume it's an ActiveRecord attribute reader.
-  #
-  def method_missing(name, *args, &block)
-    @attributes[name]
-  end
 end
 
 ##
@@ -94,15 +95,44 @@ class Landmark < ActiveRecord::Base
     write_attribute :latitude, latitude
     write_attribute :longitude, longitude
   end
+end
 
-  ##
-  # If method not found, assume it's an ActiveRecord attribute reader.
-  #
-  def method_missing(name, *args, &block)
-    @attributes[name]
+##
+# Geocoded model with block.
+#
+class Event < ActiveRecord::Base
+  geocoded_by :address do |obj,result|
+    if result
+      obj.coordinates = "#{result.latitude},#{result.longitude}"
+    end
+  end
+
+  def initialize(name, address)
+    super()
+    write_attribute :name, name
+    write_attribute :address, address
   end
 end
 
+##
+# Reverse geocoded model with block.
+#
+class Party < ActiveRecord::Base
+  reverse_geocoded_by :latitude, :longitude do |obj,result|
+    if result
+      obj.country = result.country_code
+    end
+  end
+
+  def initialize(name, latitude, longitude)
+    super()
+    write_attribute :name, name
+    write_attribute :latitude, latitude
+    write_attribute :longitude, longitude
+  end
+end
+
+
 class Test::Unit::TestCase
   def venue_params(abbrev)
     {
-- 
GitLab