Skip to content

Commit

Permalink
Fireing an event now calls aasm_error_callback if an exception is rai…
Browse files Browse the repository at this point in the history
…sed and aasm_error_callback is defined
  • Loading branch information
wildfalcon committed Dec 1, 2009
1 parent 88d4d0c commit 24b29e0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
84 changes: 46 additions & 38 deletions lib/aasm/aasm.rb
Expand Up @@ -130,12 +130,12 @@ def aasm_current_state=(state)

def aasm_determine_state_name(state)
case state
when Symbol, String
state
when Proc
state.call(self)
else
raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc."
when Symbol, String
state
when Proc
state.call(self)
else
raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc."
end
end

Expand All @@ -146,50 +146,58 @@ def aasm_state_object_for_state(name)
end

def aasm_fire_event(name, persist, *args)
old_state = aasm_state_object_for_state(aasm_current_state)
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)
old_state.call_action(:exit, self)

# new event before callback
event.call_action(:before, self)
# new event before callback
event.call_action(:before, self)

new_state_name = event.fire(self, *args)
new_state_name = event.fire(self, *args)

unless new_state_name.nil?
new_state = aasm_state_object_for_state(new_state_name)
unless new_state_name.nil?
new_state = aasm_state_object_for_state(new_state_name)

# new before_ callbacks
old_state.call_action(:before_exit, self)
new_state.call_action(:before_enter, self)
# new before_ callbacks
old_state.call_action(:before_exit, self)
new_state.call_action(:before_enter, self)

new_state.call_action(:enter, self)
new_state.call_action(:enter, self)

persist_successful = true
if persist
persist_successful = set_aasm_current_state_with_persistence(new_state_name)
event.execute_success_callback(self) if persist_successful
else
self.aasm_current_state = new_state_name
end
persist_successful = true
if persist
persist_successful = set_aasm_current_state_with_persistence(new_state_name)
event.execute_success_callback(self) if persist_successful
else
self.aasm_current_state = new_state_name
end

if persist_successful
old_state.call_action(:after_exit, self)
new_state.call_action(:after_enter, self)
event.call_action(:after, self)

if persist_successful
old_state.call_action(:after_exit, self)
new_state.call_action(:after_enter, self)
event.call_action(:after, self)
self.aasm_event_fired(name, old_state.name, self.aasm_current_state) if self.respond_to?(:aasm_event_fired)
else
self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed)
end

self.aasm_event_fired(name, old_state.name, self.aasm_current_state) if self.respond_to?(:aasm_event_fired)
persist_successful
else
self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed)
end
if self.respond_to?(:aasm_event_failed)
self.aasm_event_failed(name, old_state.name)
end

persist_successful
else
if self.respond_to?(:aasm_event_failed)
self.aasm_event_failed(name, old_state.name)
false
end
rescue => e
if self.responds_to?(:aasm_error_callback)
self.aasm_error_callback(e)
else
raise e
end

false
end
end
end
14 changes: 14 additions & 0 deletions spec/unit/aasm_spec.rb
Expand Up @@ -292,6 +292,20 @@ def @foo.aasm_event_fired(event, from, to)
@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!(:responds_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!(:responds_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
Expand Down

0 comments on commit 24b29e0

Please sign in to comment.