Skip to content

Commit

Permalink
Set state_name when state record not found [api]
Browse files Browse the repository at this point in the history
  • Loading branch information
huoxito committed Oct 30, 2013
1 parent 613cb44 commit e976a3b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
7 changes: 6 additions & 1 deletion api/app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ def self.ensure_state_id_from_api(address)
end

address.delete(:state)
address[:state_id] = Spree::State.where(search).first!.id

if state = Spree::State.where(search).first
address[:state_id] = state.id
else
address[:state_name] = search[:name] || search[:abbr]
end
rescue Exception => e
raise "#{e.message} #{search}"
end
Expand Down
16 changes: 4 additions & 12 deletions api/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,22 @@ module Spree

it 'can build an order from API with state attributes' do
ship_address.delete(:state_id)
ship_address[:state] = { 'name' => 'Alabama' }
ship_address[:state] = { 'name' => state.name }
params = { :ship_address_attributes => ship_address,
:line_items_attributes => line_items }

order = Order.build_from_api(user, params)
order.ship_address.state.name.should eq 'Alabama'
end

it 'handles state lookup exceptions' do
it 'sets state name if state record not found' do
ship_address.delete(:state_id)
ship_address[:state] = { 'name' => 'XXX' }
params = { :ship_address_attributes => ship_address,
:line_items_attributes => line_items }

expect {
order = Order.build_from_api(user, params)
}.to raise_error /XXX/
order = Order.build_from_api(user, params)
expect(order.ship_address.state_name).to eq 'XXX'
end

context 'variant not deleted' do
Expand Down Expand Up @@ -190,13 +189,6 @@ module Spree
end
end

it "raises with proper message when cant find state" do
address = { :state => { "name" => "NoNoState" } }
expect {
Order.ensure_state_id_from_api(address)
}.to raise_error /NoNoState/
end

context "shippments" do
let(:params) do
{ :shipments_attributes => [
Expand Down

0 comments on commit e976a3b

Please sign in to comment.