diff --git a/lib/geocoder/railtie.rb b/lib/geocoder/railtie.rb index 4a717eda1f8ab8a0a7e0bc906594409722c748c2..40a683f9b2b292bef20d042ae110d8488deeddf6 100644 --- a/lib/geocoder/railtie.rb +++ b/lib/geocoder/railtie.rb @@ -42,11 +42,12 @@ module Geocoder ## # Set attribute names and include the Geocoder module. # - def reverse_geocoded_by(latitude_attr, longitude_attr, options = {}) + def reverse_geocoded_by(latitude_attr, longitude_attr, options = {}, &block) geocoder_init( :fetched_address => options[:address] || :address, :latitude => latitude_attr, - :longitude => longitude_attr + :longitude => longitude_attr, + :block => block ) end diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index e13e85cc3bd2e1d9d96bed887b8b875f430bc6b8..0915e94e8a665a0783a6450346381c2873c54fe1 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -44,6 +44,19 @@ class GeocoderTest < Test::Unit::TestCase assert_equal address, v.address end + def test_geocode_fetches_and_assigns_custom_coordinates + e = Event.new(*venue_params(:msg)) + coords = [40.750354, -73.993371] + e.geocode + assert_equal coords.map{ |c| c.to_s }.join(','), e.coordinates + end + + def test_geocode_fetches_and_assigns_custom_address_components + e = Party.new(*landmark_params(:msg)) + e.geocode + assert_equal "US", e.country + end + # --- Google --- diff --git a/test/test_helper.rb b/test/test_helper.rb index ae602b6131dfd7ac2dd2b0a982547d1c1f2edaa8..569b8b283b75b2261bd4f5d0e3a7a4fa6475c4f4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -27,6 +27,14 @@ module ActiveRecord end def self.scope(*args); end + + def method_missing(name, *args, &block) + if name.to_s[-1..-1] == "=" + write_attribute name.to_s[0...-1], *args + else + read_attribute name + end + end end end @@ -73,13 +81,6 @@ class Venue < ActiveRecord::Base write_attribute :name, name write_attribute :address, address end - - ## - # If method not found, assume it's an ActiveRecord attribute reader. - # - def method_missing(name, *args, &block) - @attributes[name] - end end ## @@ -94,15 +95,44 @@ class Landmark < ActiveRecord::Base write_attribute :latitude, latitude write_attribute :longitude, longitude end +end - ## - # If method not found, assume it's an ActiveRecord attribute reader. - # - def method_missing(name, *args, &block) - @attributes[name] +## +# Geocoded model with block. +# +class Event < ActiveRecord::Base + geocoded_by :address do |obj,result| + if result + obj.coordinates = "#{result.latitude},#{result.longitude}" + end + end + + def initialize(name, address) + super() + write_attribute :name, name + write_attribute :address, address end end +## +# Reverse geocoded model with block. +# +class Party < ActiveRecord::Base + reverse_geocoded_by :latitude, :longitude do |obj,result| + if result + obj.country = result.country_code + 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 def venue_params(abbrev) {