diff --git a/lib/geocoder/stores/base.rb b/lib/geocoder/stores/base.rb index f693130a998391a7eb4260b0e6adcb60f91d411e..fd3ccaee9a23b0000314e52357daee94b6192ade 100644 --- a/lib/geocoder/stores/base.rb +++ b/lib/geocoder/stores/base.rb @@ -101,7 +101,13 @@ module Geocoder return end - query_options = [:lookup, :ip_lookup].inject({}){|hash, key| hash[key] = options[key] if options.has_key?(key); hash } + query_options = [:lookup, :ip_lookup].inject({}) do |hash, key| + if options.has_key?(key) + val = options[key] + hash[key] = val.respond_to?(:call) ? val.call(self) : val + end + hash + end results = Geocoder.search(query, query_options) # execute custom block, if specified in configuration diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index 2bd58a7a9b828cc776434d740eb0a6a11669486b..343777820f954432281d88dd42cefd8b6ff22afd 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -63,6 +63,12 @@ class GeocoderTest < Test::Unit::TestCase assert_equal Geocoder::Result::Nominatim, v.result_class end + def test_geocode_with_custom_lookup_proc_param + v = BigChurch.new(*venue_params(:msg)) + v.geocode + assert_equal Geocoder::Result::Nominatim, v.result_class + end + def test_reverse_geocode_with_custom_lookup_param v = Temple.new(*landmark_params(:msg)) v.reverse_geocode diff --git a/test/test_helper.rb b/test/test_helper.rb index 78d5c6a5e77cc55ba3ae32890596a6450a8f67ac..61e6844bc209ded85096cbeb7c4bc42ed6cb949f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -268,6 +268,27 @@ class Church < ActiveRecord::Base end end +## +# Geocoded model with custom lookup as proc. +# +class BigChurch < ActiveRecord::Base + geocoded_by :address, :lookup => lambda{|obj| obj.custom_lookup } do |obj,results| + if result = results.first + obj.result_class = result.class + end + end + + def custom_lookup + :nominatim + end + + def initialize(name, address) + super() + write_attribute :name, name + write_attribute :address, address + end +end + ## # Reverse geocoded model with custom lookup. #