Skip to content

Commit

Permalink
Refactor GeocodingUtilities#address_for_geocoding into Address#to_s(:…
Browse files Browse the repository at this point in the history
…geo). We can't get rid of the GeocodingUtilities method yet because User isn't using Address yet.
  • Loading branch information
marnen committed Jan 7, 2009
1 parent 1854c53 commit 4f9b2f7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
19 changes: 15 additions & 4 deletions app/models/address.rb
Expand Up @@ -38,10 +38,21 @@ def initialize(*args)

# Returns the #Country that the address belongs to.
def country
if @state.nil?
nil
else
@state.country
state.nil? ? nil : state.country
end

# Converts #Address to a #String.
#
# Valid values of +format+:
# <tt>:geo</tt>:: Address in comma-separated format for feeding to geocoder.
def to_s(format = :geo)
case format
when :geo
state_code = state.nil? ? nil : state.code
country_code = country.nil? ? nil : country.code
"#{street}, #{city}, #{state_code}, #{zip}, #{country_code}"
else
raise ArgumentError, "unknown format parameter: #{format}"
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/models/event.rb
Expand Up @@ -6,7 +6,7 @@ class Event < ActiveRecord::Base
belongs_to :calendar
has_many :commitments
has_many :users, :through => :commitments
composed_of :address, :mapping => [%w(street street), %w(street2 street2), %w(city city), %w(state_id state), %w(zip zip)]
composed_of :address, :mapping => [%w(street street), %w(street2 street2), %w(city city), %w(state_id state), %w(zip zip), %w(coords coords)]
# validates_presence_of :city
validates_presence_of :calendar_id
validates_presence_of :name
Expand Down Expand Up @@ -82,7 +82,7 @@ def coords
c = self[:coords]
if c.nil?
begin
c = coords_from_string(address_for_geocoding)
c = coords_from_string(address.to_s(:geo))
self[:coords] = c
self.save
rescue
Expand Down
10 changes: 7 additions & 3 deletions lib/geocoding_utilities.rb
@@ -1,9 +1,13 @@
module GeocodingUtilities
# Returns the current object's address in a form suitable for feeding to a geocoder (perhaps through coords_from_string).
def address_for_geocoding
state_code = state.nil? ? nil : state.code
country_code = country.nil? ? nil : country.code
"#{street}, #{city}, #{state_code}, #{zip}, #{country_code}"
begin
address.to_s(:geo)
rescue
state_code = state.nil? ? nil : state.code
country_code = country.nil? ? nil : country.code
"#{street}, #{city}, #{state_code}, #{zip}, #{country_code}"
end
end

# Sends the address contained in _string_ to a geocoder, and returns a #Point object with the resulting coordinates.
Expand Down
8 changes: 8 additions & 0 deletions spec/models/address_spec.rb
Expand Up @@ -107,5 +107,13 @@
Address.new(:state => mock_model(State, :country => country)).country.should == country
end
end

describe '(geographical features)' do
it "should create a string for the geocodable address parts" do
a = Address.new(:street => '123 Main Street', :street2 => '1st floor', :city => 'Anytown', :state => mock_model(State, :code => 'NY', :country => mock_model(Country, :code => 'US')), :zip => '12345', :coords => nil)
addr = a.to_s(:geo)
addr.should == "#{a.street}, #{a.city}, #{a.state.code}, #{a.zip}, #{a.country.code}"
end
end
end
end
4 changes: 2 additions & 2 deletions spec/models/event_spec.rb
Expand Up @@ -45,7 +45,7 @@
it "should be composed_of an Address" do
aggr = Event.reflect_on_aggregation(:address)
aggr.should_not be_nil
aggr.options[:mapping].should == [%w(street street), %w(street2 street2), %w(city city), %w(state_id state), %w(zip zip)]
aggr.options[:mapping].should == [%w(street street), %w(street2 street2), %w(city city), %w(state_id state), %w(zip zip), %w(coords coords)]
state = mock_model(State, :id => 15, :code => 'NY', :country => mock_model(Country, :code => 'US'))
a = Address.new
Address.should_receive(:new).and_return(a)
Expand Down Expand Up @@ -240,8 +240,8 @@

it "should create a string for the geocodable address parts" do
@event.should respond_to(:address_for_geocoding)
@event.address.should_receive(:to_s).with(:geo)
addr = @event.address_for_geocoding
addr.should == "#{@event.street}, #{@event.city}, #{@event.state.code}, #{@event.zip}, #{@event.country.code}"
end


Expand Down

0 comments on commit 4f9b2f7

Please sign in to comment.