mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Error callback for transition is now definable in aasm_event command
This commit is contained in:
parent
3cac5eb23d
commit
723622b8f8
3 changed files with 46 additions and 21 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
@ -21,6 +21,9 @@ class Foo
|
|||
def success_callback
|
||||
end
|
||||
|
||||
def error_callback(e)
|
||||
end
|
||||
|
||||
def enter
|
||||
end
|
||||
def exit
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue