Skip to content

Commit

Permalink
Make User#address_for_geocoding safe for blank addresses. [#2 state:r…
Browse files Browse the repository at this point in the history
…esolved]
  • Loading branch information
marnen committed Dec 3, 2008
1 parent 8f5ff6d commit b0d1388
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion 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.
Expand Down
9 changes: 9 additions & 0 deletions spec/models/user_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit b0d1388

Please sign in to comment.