Skip to content

Commit

Permalink
Order state changes are now tracked after all transitions, exactly li…
Browse files Browse the repository at this point in the history
…ke shipments and payment state transitions

Fixes spree#3850
  • Loading branch information
radar committed Oct 21, 2013
1 parent 68163f8 commit 3bd0aa9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
7 changes: 0 additions & 7 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,6 @@ def finalize!
updater.run_hooks

deliver_order_confirmation_email

self.state_changes.create(
previous_state: 'cart',
next_state: 'complete',
name: 'order' ,
user_id: self.user_id
)
end

def deliver_order_confirmation_email
Expand Down
8 changes: 7 additions & 1 deletion core/app/models/spree/order/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ def self.define_state_machine!
klass.next_event_transitions.each { |t| transition(t.merge(:on => :next)) }

# Persist the state on the order
after_transition do |order|
after_transition do |order, transition|
order.state = order.state
order.state_changes.create(
previous_state: transition.from,
next_state: transition.to,
name: 'order',
user_id: order.user_id
)
order.save
end

Expand Down
13 changes: 13 additions & 0 deletions core/spec/models/spree/order/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
describe Spree::Order do
let(:order) { Spree::Order.new }

def assert_state_changed(order, from, to)
state_change_exists = order.state_changes.where(:previous_state => from, :next_state => to).exists?
assert state_change_exists, "Expected order to transition from #{from} to #{to}, but didn't."
end

context "with default state machine" do
let(:transitions) do
[
Expand Down Expand Up @@ -87,6 +92,7 @@
order.line_items << FactoryGirl.create(:line_item)
order.email = "[email protected]"
order.next!
assert_state_changed(order, 'cart', 'address')
order.state.should == "address"
end

Expand Down Expand Up @@ -120,6 +126,7 @@
it "transitions to delivery" do
order.stub(:ensure_available_shipping_rates => true)
order.next!
assert_state_changed(order, 'address', 'delivery')
order.state.should == "delivery"
end

Expand All @@ -146,6 +153,7 @@
it "transitions to payment" do
order.should_receive(:set_shipments_cost)
order.next!
assert_state_changed(order, 'delivery', 'payment')
order.state.should == 'payment'
end
end
Expand Down Expand Up @@ -174,6 +182,7 @@

it "transitions to confirm" do
order.next!
assert_state_changed(order, 'payment', 'confirm')
order.state.should == "confirm"
end
end
Expand All @@ -187,6 +196,7 @@
it "transitions to complete" do
order.should_receive(:process_payments!).once.and_return true
order.next!
assert_state_changed(order, 'payment', 'complete')
order.state.should == "complete"
end
end
Expand All @@ -200,6 +210,7 @@
it "does not call process payments" do
order.should_not_receive(:process_payments!)
order.next!
assert_state_changed(order, 'payment', 'complete')
order.state.should == "complete"
end
end
Expand All @@ -222,6 +233,7 @@ class SubclassedOrder < Spree::Order
order.should_receive(:process_payments!).once
order.state = "payment"
order.next!
assert_state_changed(order, 'payment', 'complete')
order.state.should == "complete"
end
end
Expand Down Expand Up @@ -275,6 +287,7 @@ class SubclassedOrder < Spree::Order
order.should_not_receive(:payment_required?)
order.should_not_receive(:process_payments!)
order.next!
assert_state_changed(order, 'cart', 'complete')
end

end
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def compute(computable)
end

it "should log state event" do
order.state_changes.should_receive(:create).exactly(3).times #order, shipment & payment state changes
order.state_changes.should_receive(:create).exactly(2).times # shipment & payment state changes
order.finalize!
end
end
Expand Down

0 comments on commit 3bd0aa9

Please sign in to comment.