From 3bb5fe0bfe2e487bbdb2efa27d3d95b5a8b0ccb8 Mon Sep 17 00:00:00 2001 From: Wildfalcon Date: Tue, 1 Dec 2009 11:28:52 +0000 Subject: [PATCH] Error callback now propgates exception if call back is declared (in aasm_event), but not defined --- lib/aasm/aasm.rb | 8 ++------ lib/aasm/event.rb | 3 ++- spec/unit/aasm_spec.rb | 40 ++++++++++++++++++---------------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 9c7f7f7..e7271bb 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -146,9 +146,10 @@ module AASM end def aasm_fire_event(name, persist, *args) + event = self.class.aasm_events[name] begin old_state = aasm_state_object_for_state(aasm_current_state) - event = self.class.aasm_events[name] + old_state.call_action(:exit, self) @@ -194,11 +195,6 @@ module AASM end rescue StandardError => e event.execute_error_callback(self, e) - # if self.respond_to?(:aasm_error_callback) - # self.aasm_error_callback(e) - # else - # raise e - # end end end end diff --git a/lib/aasm/event.rb b/lib/aasm/event.rb index 28c54db..addb1f5 100644 --- a/lib/aasm/event.rb +++ b/lib/aasm/event.rb @@ -84,11 +84,12 @@ module AASM callback = error_callback || @error case(callback) when String, Symbol + raise error unless obj.respond_to?(callback.to_sym) obj.send(callback, error) when Proc callback.call(obj, error) when Array - callback.each{|meth|self.execute_success_callback(obj, error, meth)} + callback.each{|meth|self.execute_error_callback(obj, error, meth)} end end diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index 94b3e22..18a3ba4 100644 --- a/spec/unit/aasm_spec.rb +++ b/spec/unit/aasm_spec.rb @@ -6,7 +6,7 @@ class Foo aasm_state :open, :exit => :exit aasm_state :closed, :enter => :enter - aasm_event :close, :success => :success_callback, :error => :error_callback do + aasm_event :close, :success => :success_callback, :error => :error_callback do transitions :to => :closed, :from => [:open] end @@ -20,9 +20,6 @@ class Foo def success_callback end - - def error_callback(e) - end def enter end @@ -95,7 +92,7 @@ describe AASM, '- subclassing' do FooTwo.aasm_states.should include(state) end end - + it 'should not add the child states to the parent machine' do Foo.aasm_states.should_not include(:foo) end @@ -274,25 +271,24 @@ end describe AASM, '- event callbacks' do describe "with an error callback defined" do - before do - @foo = Foo.new - end - - - it "should run error_callback if an exception is raised" do - @foo.stub!(:enter).and_raise(StandardError) - @foo.should_receive(:error_callback) - @foo.close! - end + before do + @foo = Foo.new + end - it "should propograte an exception if error_callback is not defined" do - @foo.stub!(:enter).and_raise(StandardError) - @foo.stub!(:respond_to?).with(:error_callback).and_return(false) - @foo.should_not_receive(:error_callback) - lambda{@foo.close!}.should raise_error - end + it "should run error_callback if an exception is raised and error_callback defined" do + def @foo.error_callback(e) + end + @foo.stub!(:enter).and_raise(e=StandardError.new) + @foo.should_receive(:error_callback).with(e) + @foo.close! + end + + it "should propagrate an exception if exceptionis raised and error_callback is not defined" do + @foo.stub!(:enter).and_raise("ErrorPropagated") + lambda{@foo.close!}.should raise_error(StandardError, "ErrorPropagated") + end end - + describe "with aasm_event_fired defined" do before do @foo = Foo.new