Skip to content

Commit

Permalink
Changing signature of aasm_event_fired and aasm_event_failed.
Browse files Browse the repository at this point in the history
And ensuring that aasm_event_fired receive event, old_state and new_state
  • Loading branch information
joaovitor committed Apr 9, 2009
1 parent f2c9c28 commit bd338c7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 49 deletions.
7 changes: 4 additions & 3 deletions lib/aasm.rb
Expand Up @@ -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?
Expand All @@ -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
Expand Down
78 changes: 32 additions & 46 deletions spec/unit/aasm_spec.rb
Expand Up @@ -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

Expand Down

0 comments on commit bd338c7

Please sign in to comment.