From 5981d23f0b5980715fee48ef626b8e6bca0b7939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20Bo=CC=88ttger?= Date: Sat, 26 Nov 2011 20:34:25 +0100 Subject: [PATCH] moved exception handling into base class (away from event-fire --- lib/aasm/aasm.rb | 7 +++---- lib/aasm/supporting_classes/event.rb | 2 +- spec/unit/aasm_spec.rb | 18 ++---------------- spec/unit/event_spec.rb | 4 ++-- 4 files changed, 8 insertions(+), 23 deletions(-) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 79378d5..0a314d8 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -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) diff --git a/lib/aasm/supporting_classes/event.rb b/lib/aasm/supporting_classes/event.rb index 580b3fd..7449c39 100644 --- a/lib/aasm/supporting_classes/event.rb +++ b/lib/aasm/supporting_classes/event.rb @@ -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| diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index e8ac1ae..5f6f792 100644 --- a/spec/unit/aasm_spec.rb +++ b/spec/unit/aasm_spec.rb @@ -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 diff --git a/spec/unit/event_spec.rb b/spec/unit/event_spec.rb index d0225b6..9b4455a 100644 --- a/spec/unit/event_spec.rb +++ b/spec/unit/event_spec.rb @@ -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