Skip to content

Commit

Permalink
Make Address#state return a QuietNil if there's no state.
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Oct 20, 2009
1 parent dcf5d67 commit 53ac443
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ def initialize(*args)
@state = State.find(@state)
end
end

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

# Returns the #State that the address belongs to, or a #QuietNil if there is no state.
def state
@state || QuietNil.instance
end

# Converts #Address to a #String.
#
# Valid values of +format+:
Expand Down
17 changes: 17 additions & 0 deletions spec/models/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@
end
end

describe 'state' do
it "should be a valid instance method" do
Address.new.should respond_to(:state)
end

it "should return the state if there is one" do
state = State.make
Address.new(:state => state).state.should == state
end

it "should not complain if methods are called on it, even if nil" do
a = Address.new
a.state.should be_nil
lambda{a.state.code}.should_not raise_error
end
end

describe '(geographical features)' do
before(:each) 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)
Expand Down

0 comments on commit 53ac443

Please sign in to comment.