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

Allow separate address for reverse geocoding.

This allows the same model to be forward and reverse geocoded and still
provide a geocodable address though a non-attribute method. Also raise
an exception if sufficient configuration is not provided.
parent 9736ab57
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ ActiveRecord::Base.class_eval do ...@@ -12,7 +12,7 @@ ActiveRecord::Base.class_eval do
# #
def self.geocoded_by(address_attr, options = {}) def self.geocoded_by(address_attr, options = {})
_geocoder_init( _geocoder_init(
:address => address_attr, :user_address => address_attr,
:latitude => options[:latitude] || :latitude, :latitude => options[:latitude] || :latitude,
:longitude => options[:longitude] || :longitude :longitude => options[:longitude] || :longitude
) )
...@@ -23,7 +23,7 @@ ActiveRecord::Base.class_eval do ...@@ -23,7 +23,7 @@ ActiveRecord::Base.class_eval do
# #
def self.reverse_geocoded_by(latitude_attr, longitude_attr, options = {}) def self.reverse_geocoded_by(latitude_attr, longitude_attr, options = {})
_geocoder_init( _geocoder_init(
:address => options[:address] || :address, :fetched_address => options[:address] || :address,
:latitude => latitude_attr, :latitude => latitude_attr,
:longitude => longitude_attr :longitude => longitude_attr
) )
...@@ -44,3 +44,10 @@ ActiveRecord::Base.class_eval do ...@@ -44,3 +44,10 @@ ActiveRecord::Base.class_eval do
included_modules.include? Geocoder::ActiveRecord included_modules.include? Geocoder::ActiveRecord
end end
end end
class GeocoderError < StandardError
end
class GeocoderConfigurationError < GeocoderError
end
...@@ -183,9 +183,13 @@ module Geocoder ...@@ -183,9 +183,13 @@ module Geocoder
# coordinates as an array: <tt>[lat, lon]</tt>. # coordinates as an array: <tt>[lat, lon]</tt>.
# #
def fetch_coordinates(save = false) def fetch_coordinates(save = false)
coords = Geocoder::Lookup.coordinates( address_method = self.class.geocoder_options[:user_address]
send(self.class.geocoder_options[:address]) unless address_method.is_a? Symbol
) raise GeocoderConfigurationError,
"You are attempting to fetch coordinates but have not specified " +
"a method which provides an address for the object."
end
coords = Geocoder::Lookup.coordinates(send(address_method))
unless coords.blank? unless coords.blank?
method = (save ? "update" : "write") + "_attribute" method = (save ? "update" : "write") + "_attribute"
send method, self.class.geocoder_options[:latitude], coords[0] send method, self.class.geocoder_options[:latitude], coords[0]
...@@ -206,13 +210,17 @@ module Geocoder ...@@ -206,13 +210,17 @@ module Geocoder
# address as a string. # address as a string.
# #
def fetch_address(save = false) def fetch_address(save = false)
address = Geocoder::Lookup.address( lat_attr = self.class.geocoder_options[:latitude]
send(self.class.geocoder_options[:latitude]), lon_attr = self.class.geocoder_options[:longitude]
send(self.class.geocoder_options[:longitude]) unless lat_attr.is_a?(Symbol) and lon_attr.is_a?(Symbol)
) raise GeocoderConfigurationError,
"You are attempting to fetch an address but have not specified " +
"attributes which provide coordinates for the object."
end
address = Geocoder::Lookup.address(send(lat_attr), send(lon_attr))
unless address.blank? unless address.blank?
method = (save ? "update" : "write") + "_attribute" method = (save ? "update" : "write") + "_attribute"
send method, self.class.geocoder_options[:address], address send method, self.class.geocoder_options[:fetched_address], address
end end
address address
end end
......
...@@ -20,4 +20,11 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -20,4 +20,11 @@ class GeocoderTest < Test::Unit::TestCase
assert_equal [0.0, 1.0], assert_equal [0.0, 1.0],
Geocoder::Calculations.geographic_center([[0,0], [0,1], [0,2]]) Geocoder::Calculations.geographic_center([[0,0], [0,1], [0,2]])
end end
def test_exception_raised_for_unconfigured_geocoding
l = Landmark.new("Mount Rushmore", 43.88, -103.46)
assert_raises GeocoderConfigurationError do
l.fetch_coordinates
end
end
end end
...@@ -65,6 +65,27 @@ class Venue < ActiveRecord::Base ...@@ -65,6 +65,27 @@ class Venue < ActiveRecord::Base
end end
end end
##
# Reverse geocoded model.
#
class Landmark < ActiveRecord::Base
reverse_geocoded_by :latitude, :longitude
def initialize(name, latitude, longitude)
super()
write_attribute :name, name
write_attribute :latitude, latitude
write_attribute :longitude, longitude
end
##
# If method not found, assume it's an ActiveRecord attribute reader.
#
def method_missing(name, *args, &block)
@attributes[name]
end
end
class Test::Unit::TestCase class Test::Unit::TestCase
def venue_params(abbrev) def venue_params(abbrev)
{ {
......
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