Skip to content
Snippets Groups Projects
Commit 253af9de authored by Alex Reisner's avatar Alex Reisner
Browse files

Allow passing a block to 'geocoded_by' in model.

For advanced/custom handling of gecoding result data.
parent 302dfca6
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment