From bd338c76efa95f110a4c4615098ee5ebacd15f6b Mon Sep 17 00:00:00 2001 From: Joao Vitor Date: Thu, 9 Apr 2009 02:23:02 -0300 Subject: [PATCH] Changing signature of aasm_event_fired and aasm_event_failed. And ensuring that aasm_event_fired receive event, old_state and new_state --- lib/aasm.rb | 7 ++-- spec/unit/aasm_spec.rb | 78 +++++++++++++++++------------------------- 2 files changed, 36 insertions(+), 49 deletions(-) diff --git a/lib/aasm.rb b/lib/aasm.rb index 6512ec89..972cc6c2 100644 --- a/lib/aasm.rb +++ b/lib/aasm.rb @@ -128,6 +128,7 @@ def aasm_state_object_for_state(name) def aasm_fire_event(name, persist, *args) aasm_state_object_for_state(aasm_current_state).call_action(:exit, self) + old_state = self.aasm_current_state new_state = self.class.aasm_events[name].fire(self, *args) unless new_state.nil? @@ -142,15 +143,15 @@ def aasm_fire_event(name, persist, *args) end if persist_successful - self.aasm_event_fired(self.aasm_current_state, new_state) if self.respond_to?(:aasm_event_fired) + self.aasm_event_fired(name, old_state, self.aasm_current_state) if self.respond_to?(:aasm_event_fired) else - self.aasm_event_failed(name) if self.respond_to?(:aasm_event_failed) + self.aasm_event_failed(name, old_state) if self.respond_to?(:aasm_event_failed) end persist_successful else if self.respond_to?(:aasm_event_failed) - self.aasm_event_failed(name) + self.aasm_event_failed(name, old_state) end false diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index 6ca181ee..81576ee1 100644 --- a/spec/unit/aasm_spec.rb +++ b/spec/unit/aasm_spec.rb @@ -255,66 +255,52 @@ def foo.aasm_read_state end describe AASM, '- event callbacks' do - it 'should call aasm_event_fired if defined and successful for bang fire' do - foo = Foo.new - def foo.aasm_event_fired(from, to) + describe "with aasm_event_fired defined" do + before do + @foo = Foo.new + def @foo.aasm_event_fired(event, from, to) + end end - foo.should_receive(:aasm_event_fired) - - foo.close! - end - - it 'should not call aasm_event_fired if defined but persist fails for bang fire' do - foo = Foo.new - def foo.aasm_event_fired(from, to) + it 'should call it for successful bang fire' do + @foo.should_receive(:aasm_event_fired).with(:close, :open, :closed) + @foo.close! end - foo.stub!(:set_aasm_current_state_with_persistence).and_return(false) - - foo.should_not_receive(:aasm_event_fired) - foo.close! - end - - it 'should not call aasm_event_failed if defined and persist fails for bang fire' do - foo = Foo.new - def foo.aasm_event_failed(from, to) + it 'should call it for successful non-bang fire' do + @foo.should_receive(:aasm_event_fired) + @foo.close end - foo.stub!(:set_aasm_current_state_with_persistence).and_return(false) - - foo.should_receive(:aasm_event_failed) - - foo.close! - end - it 'should call aasm_event_fired if defined and successful for non-bang fire' do - foo = Foo.new - def foo.aasm_event_fired(from, to) + it 'should not call it for failing bang fire' do + @foo.stub!(:set_aasm_current_state_with_persistence).and_return(false) + @foo.should_not_receive(:aasm_event_fired) + @foo.close! end - - foo.should_receive(:aasm_event_fired) - - foo.close end - it 'should call aasm_event_failed if defined and transition failed for bang fire' do - foo = Foo.new - def foo.aasm_event_failed(event) + describe "with aasm_event_failed defined" do + before do + @foo = Foo.new + def @foo.aasm_event_failed(event, from) + end end - foo.should_receive(:aasm_event_failed) - - foo.null! - end - - it 'should call aasm_event_failed if defined and transition failed for non-bang fire' do - foo = Foo.new - def foo.aasm_event_failed(event) + it 'should call it when transition failed for bang fire' do + @foo.should_receive(:aasm_event_failed).with(:null, :open) + @foo.null! end - foo.should_receive(:aasm_event_failed) + it 'should call it when transition failed for non-bang fire' do + @foo.should_receive(:aasm_event_failed).with(:null, :open) + @foo.null + end - foo.null + it 'should not call it if persist fails for bang fire' do + @foo.stub!(:set_aasm_current_state_with_persistence).and_return(false) + @foo.should_receive(:aasm_event_failed) + @foo.close! + end end end