1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

Error callback now propgates exception if call back is declared (in aasm_event), but not defined

This commit is contained in:
Wildfalcon 2009-12-01 11:28:52 +00:00
parent 723622b8f8
commit 3bb5fe0bfe
3 changed files with 22 additions and 29 deletions

View file

@ -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

View file

@ -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

View file

@ -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