Skip to content
Snippets Groups Projects
Commit 81f2ea0d authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge branch 'always_execute_geocode_block'

parents 60ee62ed 1f04dff8
No related branches found
No related tags found
No related merge requests found
......@@ -217,12 +217,13 @@ module Geocoder::Store
#
def geocode
do_lookup(false) do |o,rs|
r = rs.first
unless r.latitude.nil? or r.longitude.nil?
o.send :write_attribute, self.class.geocoder_options[:latitude], r.latitude
o.send :write_attribute, self.class.geocoder_options[:longitude], r.longitude
if r = rs.first
unless r.latitude.nil? or r.longitude.nil?
o.send :write_attribute, self.class.geocoder_options[:latitude], r.latitude
o.send :write_attribute, self.class.geocoder_options[:longitude], r.longitude
end
r.coordinates
end
r.coordinates
end
end
......
......@@ -98,18 +98,17 @@ module Geocoder
return
end
if (results = Geocoder.search(query)).size > 0
results = Geocoder.search(query)
# execute custom block, if specified in configuration
block_key = reverse ? :reverse_block : :geocode_block
if custom_block = options[block_key]
custom_block.call(self, results)
# execute custom block, if specified in configuration
block_key = reverse ? :reverse_block : :geocode_block
if custom_block = options[block_key]
custom_block.call(self, results)
# else execute block passed directly to this method,
# which generally performs the "auto-assigns"
elsif block_given?
yield(self, results)
end
# else execute block passed directly to this method,
# which generally performs the "auto-assigns"
elsif block_given?
yield(self, results)
end
end
end
......
......@@ -56,11 +56,12 @@ module Geocoder::Store
#
def geocode
do_lookup(false) do |o,rs|
r = rs.first
unless r.coordinates.nil?
o.send :write_attribute, self.class.geocoder_options[:coordinates], r.coordinates.reverse
if r = rs.first
unless r.coordinates.nil?
o.send :write_attribute, self.class.geocoder_options[:coordinates], r.coordinates.reverse
end
r.coordinates
end
r.coordinates
end
end
......
......@@ -32,6 +32,12 @@ class GeocoderTest < Test::Unit::TestCase
assert_equal coords, [v.latitude, v.longitude]
end
def test_geocode_block_executed_when_no_results
v = Event.new("Nowhere", "no results")
v.geocode
assert_equal "NOT FOUND", v.coords_string
end
def test_reverse_geocode_assigns_and_returns_address
v = Landmark.new(*landmark_params(:msg))
address = "4 Penn Plaza, New York, NY 10001, USA"
......
......@@ -200,6 +200,8 @@ class Event < ActiveRecord::Base
geocoded_by :address do |obj,results|
if result = results.first
obj.coords_string = "#{result.latitude},#{result.longitude}"
else
obj.coords_string = "NOT FOUND"
end
end
......
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