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

Allow passing block to reverse_geocoded_by.

parent d88a46e3
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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 ---
......
......@@ -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)
{
......
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