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
|
false
|
||||||
end
|
end
|
||||||
rescue => e
|
rescue StandardError => e
|
||||||
if self.respond_to?(:aasm_error_callback)
|
event.execute_error_callback(self, e)
|
||||||
self.aasm_error_callback(e)
|
# if self.respond_to?(:aasm_error_callback)
|
||||||
else
|
# self.aasm_error_callback(e)
|
||||||
raise e
|
# else
|
||||||
end
|
# raise e
|
||||||
|
# end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -58,6 +58,9 @@ module AASM
|
||||||
if options.key?(:success) then
|
if options.key?(:success) then
|
||||||
@success = options[:success]
|
@success = options[:success]
|
||||||
end
|
end
|
||||||
|
if options.key?(:error) then
|
||||||
|
@error = options[:error]
|
||||||
|
end
|
||||||
if block then
|
if block then
|
||||||
instance_eval(&block)
|
instance_eval(&block)
|
||||||
end
|
end
|
||||||
|
@ -77,6 +80,18 @@ module AASM
|
||||||
end
|
end
|
||||||
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
|
private
|
||||||
|
|
||||||
def _call_action(action, record)
|
def _call_action(action, record)
|
||||||
|
|
|
@ -6,7 +6,7 @@ class Foo
|
||||||
aasm_state :open, :exit => :exit
|
aasm_state :open, :exit => :exit
|
||||||
aasm_state :closed, :enter => :enter
|
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]
|
transitions :to => :closed, :from => [:open]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -20,6 +20,9 @@ class Foo
|
||||||
|
|
||||||
def success_callback
|
def success_callback
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def error_callback(e)
|
||||||
|
end
|
||||||
|
|
||||||
def enter
|
def enter
|
||||||
end
|
end
|
||||||
|
@ -270,6 +273,26 @@ describe AASM, '- getting events for a state' do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe AASM, '- event callbacks' do
|
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
|
describe "with aasm_event_fired defined" do
|
||||||
before do
|
before do
|
||||||
@foo = Foo.new
|
@foo = Foo.new
|
||||||
|
@ -292,20 +315,6 @@ describe AASM, '- event callbacks' do
|
||||||
@foo.should_not_receive(:aasm_event_fired)
|
@foo.should_not_receive(:aasm_event_fired)
|
||||||
@foo.close!
|
@foo.close!
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "with aasm_event_failed defined" do
|
describe "with aasm_event_failed defined" do
|
||||||
|
|
Loading…
Reference in a new issue