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
#
def self.geocoded_by(address_attr, options = {})
_geocoder_init(
:address => address_attr,
:user_address => address_attr,
:latitude => options[:latitude] || :latitude,
:longitude => options[:longitude] || :longitude
)
......@@ -23,7 +23,7 @@ ActiveRecord::Base.class_eval do
#
def self.reverse_geocoded_by(latitude_attr, longitude_attr, options = {})
_geocoder_init(
:address => options[:address] || :address,
:fetched_address => options[:address] || :address,
:latitude => latitude_attr,
:longitude => longitude_attr
)
......@@ -44,3 +44,10 @@ ActiveRecord::Base.class_eval do
included_modules.include? Geocoder::ActiveRecord
end
end
class GeocoderError < StandardError
end
class GeocoderConfigurationError < GeocoderError
end
......@@ -183,9 +183,13 @@ module Geocoder
# coordinates as an array: <tt>[lat, lon]</tt>.
#
def fetch_coordinates(save = false)
coords = Geocoder::Lookup.coordinates(
send(self.class.geocoder_options[:address])
)
address_method = self.class.geocoder_options[:user_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?
method = (save ? "update" : "write") + "_attribute"
send method, self.class.geocoder_options[:latitude], coords[0]
......@@ -206,13 +210,17 @@ module Geocoder
# address as a string.
#
def fetch_address(save = false)
address = Geocoder::Lookup.address(
send(self.class.geocoder_options[:latitude]),
send(self.class.geocoder_options[:longitude])
)
lat_attr = self.class.geocoder_options[:latitude]
lon_attr = 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?
method = (save ? "update" : "write") + "_attribute"
send method, self.class.geocoder_options[:address], address
send method, self.class.geocoder_options[:fetched_address], address
end
address
end
......
......@@ -20,4 +20,11 @@ class GeocoderTest < Test::Unit::TestCase
assert_equal [0.0, 1.0],
Geocoder::Calculations.geographic_center([[0,0], [0,1], [0,2]])
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
......@@ -65,6 +65,27 @@ class Venue < ActiveRecord::Base
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
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