From 01f703d7f2b171ca64aa81ce75b79bb4e094782e Mon Sep 17 00:00:00 2001 From: Alan Kennedy Date: Tue, 20 May 2014 18:37:47 +0100 Subject: [PATCH] New transition visible after created inside transaction --- lib/statesman/adapters/active_record.rb | 1 + lib/statesman/adapters/mongoid.rb | 1 + spec/statesman/adapters/shared_examples.rb | 11 +++++++++++ 3 files changed, 13 insertions(+) diff --git a/lib/statesman/adapters/active_record.rb b/lib/statesman/adapters/active_record.rb index 58015af9..d03da57b 100644 --- a/lib/statesman/adapters/active_record.rb +++ b/lib/statesman/adapters/active_record.rb @@ -24,6 +24,7 @@ def create(from, to, metadata = {}) ::ActiveRecord::Base.transaction do @observer.execute(:before, from, to, transition) transition.save! + @last_transition = transition @observer.execute(:after, from, to, transition) @last_transition = nil end diff --git a/lib/statesman/adapters/mongoid.rb b/lib/statesman/adapters/mongoid.rb index 27e6aec8..390ad161 100644 --- a/lib/statesman/adapters/mongoid.rb +++ b/lib/statesman/adapters/mongoid.rb @@ -22,6 +22,7 @@ def create(from, to, metadata = {}) @observer.execute(:before, from, to, transition) transition.save! + @last_transition = transition @observer.execute(:after, from, to, transition) @observer.execute(:after_commit, from, to, transition) @last_transition = nil diff --git a/spec/statesman/adapters/shared_examples.rb b/spec/statesman/adapters/shared_examples.rb index cfeb823d..cbe40732 100644 --- a/spec/statesman/adapters/shared_examples.rb +++ b/spec/statesman/adapters/shared_examples.rb @@ -29,6 +29,7 @@ describe "#create" do let(:from) { :x } let(:to) { :y } + let(:there) { :z } let(:create) { adapter.create(from, to) } subject { -> { create } } @@ -71,6 +72,16 @@ end.once adapter.create(from, to) end + + it "exposes the new transition for subsequent transitions" do + adapter.create(from, to) + + observer.should_receive(:execute).with do + |phase, from_state, to_state, transition| + expect(adapter.last).to eq(transition) if phase == :after + end.once + adapter.create(to, there) + end end context "with metadata" do