diff --git a/lib/geocoder/active_record.rb b/lib/geocoder/active_record.rb index fdb38c73120147045c34a205d35bd6af056ee38e..018140d5bacd6ceaee0bed51e45693464eb16b5c 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 20bad2412df726763add39ad3cda228867700ca6..5c2296886186b4b17c5dc76583db31b2a90deeaa 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 61dfb3136bc0ac78aefa095e3d53a425afa7c4d8..a0a9ab679c28f047f69ef5f03859a2449e87e781 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 b2017a9d29a2bde1452ecf8bd9c9931b85fc55d8..f621cea7d7c6c2d0986a3a5398543ce5afd43f7a 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