Skip to content
Snippets Groups Projects
Commit 8c3827d0 authored by Bonias's avatar Bonias
Browse files

lookup option for ActiveRecord::Base.geocoded_by can be a proc

  Example:

    class City < ActiveRecord::Base
      geocoded_by :address, :lookup => lambda{|obj| obj.custom_lookup }

      def custom_lookup
        # return custom lookup for record
      end
    end
parent bf2f584c
No related branches found
No related tags found
No related merge requests found
...@@ -101,7 +101,13 @@ module Geocoder ...@@ -101,7 +101,13 @@ module Geocoder
return return
end 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) results = Geocoder.search(query, query_options)
# execute custom block, if specified in configuration # execute custom block, if specified in configuration
......
...@@ -63,6 +63,12 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -63,6 +63,12 @@ class GeocoderTest < Test::Unit::TestCase
assert_equal Geocoder::Result::Nominatim, v.result_class assert_equal Geocoder::Result::Nominatim, v.result_class
end 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 def test_reverse_geocode_with_custom_lookup_param
v = Temple.new(*landmark_params(:msg)) v = Temple.new(*landmark_params(:msg))
v.reverse_geocode v.reverse_geocode
......
...@@ -268,6 +268,27 @@ class Church < ActiveRecord::Base ...@@ -268,6 +268,27 @@ class Church < ActiveRecord::Base
end end
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. # Reverse geocoded model with custom lookup.
# #
......
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