diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 85252b0..9c7f7f7 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -192,12 +192,13 @@ module AASM false end - rescue => e - if self.respond_to?(:aasm_error_callback) - self.aasm_error_callback(e) - else - raise e - 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 e4c2cfe..28c54db 100644 --- a/lib/aasm/event.rb +++ b/lib/aasm/event.rb @@ -58,6 +58,9 @@ module AASM if options.key?(:success) then @success = options[:success] end + if options.key?(:error) then + @error = options[:error] + end if block then instance_eval(&block) end @@ -77,6 +80,18 @@ module AASM end end + def execute_error_callback(obj, error, error_callback=nil) + callback = error_callback || @error + case(callback) + when String, Symbol + obj.send(callback, error) + when Proc + callback.call(obj, error) + when Array + callback.each{|meth|self.execute_success_callback(obj, error, meth)} + end + end + private def _call_action(action, record) diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index f486162..94b3e22 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 do + aasm_event :close, :success => :success_callback, :error => :error_callback do transitions :to => :closed, :from => [:open] end @@ -20,6 +20,9 @@ class Foo def success_callback end + + def error_callback(e) + end def enter end @@ -270,6 +273,26 @@ describe AASM, '- getting events for a state' do 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 + + 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 + end + describe "with aasm_event_fired defined" do before do @foo = Foo.new @@ -292,20 +315,6 @@ describe AASM, '- event callbacks' do @foo.should_not_receive(:aasm_event_fired) @foo.close! end - - it "should run aasm_error_callback if an exception is raised" do - @foo.stub!(:enter).and_raise(StandardError) - @foo.stub!(:respond_to?).with(:aasm_error_callback).and_return(true) - @foo.should_receive(:aasm_error_callback) - @foo.close! - end - - it "should propograte an exception if aasm_error_callback is not defined" do - @foo.stub!(:enter).and_raise(StandardError) - @foo.stub!(:respond_to?).with(:aasm_error_callback).and_return(false) - @foo.should_not_receive(:aasm_error_callback) - lambda{@foo.close!}.should raise_error - end end describe "with aasm_event_failed defined" do