From b0d138897e46ad7ec35a68cdc7d1ec04f6115224 Mon Sep 17 00:00:00 2001 From: Marnen Laibow-Koser Date: Wed, 3 Dec 2008 13:22:18 -0500 Subject: [PATCH] Make User#address_for_geocoding safe for blank addresses. [#2 state:resolved] --- app/models/user.rb | 2 +- lib/geocoding_utilities.rb | 4 +++- spec/models/user_spec.rb | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 167fb8d2..31d42b34 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -33,7 +33,7 @@ def admin? end def country - return self.state.country + return self.state.nil? ? nil : self.state.country end def to_s diff --git a/lib/geocoding_utilities.rb b/lib/geocoding_utilities.rb index ccf13af9..878a476e 100644 --- a/lib/geocoding_utilities.rb +++ b/lib/geocoding_utilities.rb @@ -1,7 +1,9 @@ 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 - "#{street}, #{city}, #{state.code}, #{zip}, #{country.code}" + state_code = state.nil? ? nil : state.code + country_code = country.nil? ? nil : country.code + "#{street}, #{city}, #{state_code}, #{zip}, #{country_code}" end # Sends the address contained in _string_ to a geocoder, and returns a #Point object with the resulting coordinates. diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3043462d..f1f88507 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -172,6 +172,15 @@ @user.should_not_receive(:save) @user.coords end + + it "should provide a valid string for address_for_geocoding, even if the user's address is invalid" do + lambda {@user.address_for_geocoding}.should_not raise_error + @user.address_for_geocoding.should be_a_kind_of(String) + @blank = User.new # invalid address, since it's blank! + lambda {@blank.address_for_geocoding}.should_not raise_error + @blank.address_for_geocoding.should be_a_kind_of(String) + @blank.address_for_geocoding.should =~ /^[\s,]*$/ # just spaces and commas + end end describe User, "(authentication structure)" do