Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'always_execute_geocode_block'

  • Loading branch information...
commit 81f2ea0dbcb85de63c5d494c569c2dad42740e2f 2 parents 60ee62e + 1f04dff
@alexreisner authored
View
11 lib/geocoder/stores/active_record.rb
@@ -217,12 +217,13 @@ def add_exclude_condition(conditions, exclude)
#
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
View
19 lib/geocoder/stores/base.rb
@@ -98,18 +98,17 @@ def do_lookup(reverse = false)
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
View
9 lib/geocoder/stores/mongo_base.rb
@@ -56,11 +56,12 @@ def to_coordinates
#
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
View
6 test/geocoder_test.rb
@@ -32,6 +32,12 @@ def test_geocode_assigns_and_returns_coordinates
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"
View
2  test/test_helper.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.