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

Rewrite geocode method (refactor + minor changes).

Don't take 'reverse' argument, instead figure it out by looking at what
options were specified for the model. Also, don't raise exceptions on
bad configuration (no longer possible to detect, wasn't that important).
parent fc818cc9
No related branches found
No related tags found
No related merge requests found
......@@ -171,7 +171,7 @@ module Geocoder::Orm
# address as a string.
#
def fetch_address(save = false)
geocode(true) do |r|
geocode do |r|
unless r.address.nil?
method = (save ? "update" : "write") + "_attribute"
send method, self.class.geocoder_options[:fetched_address], r.address
......
......@@ -43,29 +43,19 @@ module Geocoder
# Look up geographic data based on object attributes,
# and do something with it (requires a block).
#
def geocode(reverse = false, &block)
if reverse
lat_attr = self.class.geocoder_options[:latitude]
lon_attr = self.class.geocoder_options[:longitude]
unless lat_attr.is_a?(Symbol) and lon_attr.is_a?(Symbol)
raise Geocoder::ConfigurationError,
"You are attempting to fetch an address but have not specified " +
"attributes which provide coordinates for the object."
end
args = [send(lat_attr), send(lon_attr)]
else
address_method = self.class.geocoder_options[:user_address]
unless address_method.is_a? Symbol
raise Geocoder::ConfigurationError,
"You are attempting to geocode an object but have not specified " +
"a method which provides an address to search for."
end
args = [send(address_method)]
end
def geocode
options = self.class.geocoder_options
args = options[:user_address] ?
[:user_address] : [:latitude, :longitude]
args.map!{ |a| send(options[a]) }
# 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
b.call(result)
if block_given?
yield(result)
else
self.class.geocoder_options[:block].call(result)
end
end
end
end
......
......@@ -20,13 +20,6 @@ class GeocoderTest < Test::Unit::TestCase
Geocoder::Calculations.geographic_center([[0,0], [0,1], [0,2]])
end
def test_exception_raised_for_unconfigured_geocoding
l = Landmark.new("Mount Rushmore", 43.88, -103.46)
assert_raises Geocoder::ConfigurationError do
l.fetch_coordinates
end
end
def test_does_not_choke_on_nil_address
v = Venue.new("Venue", nil)
assert_nothing_raised do
......
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