Skip to content

Commit

Permalink
Merge pull request alexreisner#572 from Bonias/allow-to-set-custom-lo…
Browse files Browse the repository at this point in the history
…okup-per-model

Add :lookup option to Geocoder.search.

Allows lookup to be specified on a per-model basis.
  • Loading branch information
alexreisner committed Dec 28, 2013
2 parents 50e3d9e + 8c3827d commit 0694cd5
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/geocoder/models/active_record.rb
Expand Up @@ -16,7 +16,8 @@ def geocoded_by(address_attr, options = {}, &block)
:longitude => options[:longitude] || :longitude,
:geocode_block => block,
:units => options[:units],
:method => options[:method]
:method => options[:method],
:lookup => options[:lookup]
)
end

Expand All @@ -30,8 +31,9 @@ def reverse_geocoded_by(latitude_attr, longitude_attr, options = {}, &block)
:latitude => latitude_attr,
:longitude => longitude_attr,
:reverse_block => block,
:units => options[:units],
:method => options[:method]
:units => options[:units],
:method => options[:method],
:lookup => options[:lookup]
)
end

Expand Down
6 changes: 4 additions & 2 deletions lib/geocoder/models/mongo_base.rb
Expand Up @@ -19,7 +19,8 @@ def geocoded_by(address_attr, options = {}, &block)
:geocode_block => block,
:units => options[:units],
:method => options[:method],
:skip_index => options[:skip_index] || false
:skip_index => options[:skip_index] || false,
:lookup => options[:lookup]
)
end

Expand All @@ -34,7 +35,8 @@ def reverse_geocoded_by(coordinates_attr, options = {}, &block)
:reverse_block => block,
:units => options[:units],
:method => options[:method],
:skip_index => options[:skip_index] || false
:skip_index => options[:skip_index] || false,
:lookup => options[:lookup]
)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/geocoder/query.rb
Expand Up @@ -33,9 +33,9 @@ def sanitized_text
#
def lookup
if ip_address?
name = Configuration.ip_lookup || Geocoder::Lookup.ip_services.first
name = options[:ip_lookup] || Configuration.ip_lookup || Geocoder::Lookup.ip_services.first
else
name = Configuration.lookup || Geocoder::Lookup.street_services.first
name = options[:lookup] || Configuration.lookup || Geocoder::Lookup.street_services.first
end
Lookup.get(name)
end
Expand Down
9 changes: 8 additions & 1 deletion lib/geocoder/stores/base.rb
Expand Up @@ -101,7 +101,14 @@ def do_lookup(reverse = false)
return
end

results = Geocoder.search(query)
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
block_key = reverse ? :reverse_block : :geocode_block
Expand Down
18 changes: 18 additions & 0 deletions test/geocoder_test.rb
Expand Up @@ -56,4 +56,22 @@ def test_forward_and_reverse_geocoding_on_same_model_works
g.reverse_geocode
assert_not_nil g.location
end

def test_geocode_with_custom_lookup_param
v = Church.new(*venue_params(:msg))
v.geocode
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
assert_equal Geocoder::Result::Nominatim, v.result_class
end
end
5 changes: 5 additions & 0 deletions test/query_test.rb
Expand Up @@ -42,4 +42,9 @@ def test_sanitized_text_with_array
q = Geocoder::Query.new([43.1313,11.3131])
assert_equal "43.1313,11.3131", q.sanitized_text
end

def test_custom_lookup
query = Geocoder::Query.new("address", :lookup => :nominatim)
assert_equal Geocoder::Lookup::Nominatim, query.lookup.class
end
end
56 changes: 56 additions & 0 deletions test/test_helper.rb
Expand Up @@ -251,6 +251,62 @@ def initialize(name)
end
end

##
# Geocoded model with custom lookup.
#
class Church < ActiveRecord::Base
geocoded_by :address, :lookup => :nominatim do |obj,results|
if result = results.first
obj.result_class = result.class
end
end

def initialize(name, address)
super()
write_attribute :name, name
write_attribute :address, address
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.
#
class Temple < ActiveRecord::Base
reverse_geocoded_by :latitude, :longitude, :lookup => :nominatim do |obj,results|
if result = results.first
obj.result_class = result.class
end
end

def initialize(name, latitude, longitude)
super()
write_attribute :name, name
write_attribute :latitude, latitude
write_attribute :longitude, longitude
end
end


class Test::Unit::TestCase

Expand Down

0 comments on commit 0694cd5

Please sign in to comment.