1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

moved exception handling into base class (away from event-fire

This commit is contained in:
Thorsten Böttger 2011-11-26 20:34:25 +01:00
parent 02b6cfdc86
commit 5981d23f0b
4 changed files with 8 additions and 23 deletions

View file

@ -163,9 +163,7 @@ private
# new event before callback
event.fire_callbacks(:before, self)
new_state_name = event.fire(self, *args)
unless new_state_name.nil?
if new_state_name = event.fire(self, *args)
new_state = aasm_state_object_for_state(new_state_name)
# new before_ callbacks
@ -193,12 +191,13 @@ private
end
persist_successful
else
if self.respond_to?(:aasm_event_failed)
self.aasm_event_failed(name, old_state.name)
end
false
raise AASM::InvalidTransition, "Event '#{event.name}' cannot transition from '#{self.aasm_current_state}'"
end
rescue StandardError => e
event.execute_error_callback(self, e)

View file

@ -29,7 +29,7 @@ module AASM
def fire(obj, to_state=nil, *args)
transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
raise AASM::InvalidTransition, "Event '#{name}' cannot transition from '#{obj.aasm_current_state}'" if transitions.size == 0
return nil if transitions.size == 0
next_state = nil
transitions.each do |transition|

View file

@ -104,13 +104,6 @@ describe AASM, '- initial states' do
end
describe AASM, '- event firing with persistence' do
it 'should fire the Event' do
foo = Foo.new
Foo.aasm_events[:close].should_receive(:fire).with(foo)
foo.close!
end
it 'should update the current state' do
foo = Foo.new
foo.close!
@ -172,13 +165,6 @@ describe AASM, '- event firing with persistence' do
end
describe AASM, '- event firing without persistence' do
it 'should fire the Event' do
foo = Foo.new
Foo.aasm_events[:close].should_receive(:fire).with(foo)
foo.close
end
it 'should update the current state' do
foo = Foo.new
foo.close
@ -289,12 +275,12 @@ describe AASM, '- event callbacks' do
it 'should call it when transition failed for bang fire' do
@foo.should_receive(:aasm_event_failed).with(:null, :open)
@foo.null!
lambda {@foo.null!}.should raise_error(AASM::InvalidTransition)
end
it 'should call it when transition failed for non-bang fire' do
@foo.should_receive(:aasm_event_failed).with(:null, :open)
@foo.null
lambda {@foo.null}.should raise_error(AASM::InvalidTransition)
end
it 'should not call it if persist fails for bang fire' do

View file

@ -30,12 +30,12 @@ describe AASM::SupportingClasses::Event do
end
describe AASM::SupportingClasses::Event, 'when firing an event' do
it 'should raise an AASM::InvalidTransition error if the transitions are empty' do
it 'should return nil if the transitions are empty' do
obj = mock('object')
obj.stub!(:aasm_current_state)
event = AASM::SupportingClasses::Event.new(:event)
lambda { event.fire(obj) }.should raise_error(AASM::InvalidTransition)
event.fire(obj).should be_nil
end
it 'should return the state of the first matching transition it finds' do