From 574879ef2337e53d4319d544486de6ddfad11afb Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Sat, 5 Mar 2011 16:04:46 -0500 Subject: [PATCH] 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). --- lib/geocoder/orms/active_record.rb | 2 +- lib/geocoder/orms/base.rb | 32 ++++++++++-------------------- test/geocoder_test.rb | 7 ------- 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/lib/geocoder/orms/active_record.rb b/lib/geocoder/orms/active_record.rb index fe439af9..b1bac032 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 04d57597..1edf7e45 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 1a7dc97c..e13e85cc 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 -- GitLab