diff --git a/lib/geocoder/orms/active_record.rb b/lib/geocoder/orms/active_record.rb index fe439af9dab1164b0b7eaef08c5607cd05143d7f..b1bac032aa8a6b9d53c64635c6722c6bcfba624e 100644 --- a/lib/geocoder/orms/active_record.rb +++ b/lib/geocoder/orms/active_record.rb @@ -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 diff --git a/lib/geocoder/orms/base.rb b/lib/geocoder/orms/base.rb index 04d57597d9c4931e2846e4b9f611e2651c331119..1edf7e458b057dec41064803869b04815eb61d96 100644 --- a/lib/geocoder/orms/base.rb +++ b/lib/geocoder/orms/base.rb @@ -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 diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index 1a7dc97cd3981905af467cdcd63558f281f65617..e13e85cc3bd2e1d9d96bed887b8b875f430bc6b8 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -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