Support enter and exit actions on states

This commit is contained in:
Scott Barron 2008-05-31 15:08:12 -07:00
parent 2bb30ee84b
commit 3ccbeaca88
4 changed files with 32 additions and 3 deletions

View File

@ -1,3 +1,5 @@
* Support enter and exit actions on states
* Use named_scope in AR persistence layer, if available [Jan De Poorter]
* Incremented version number

1
TODO
View File

@ -1,6 +1,5 @@
Before Next Release:
* Add state actions (enter, exit, after)
* Add #aasm_next_state_for_event
* Add #aasm_next_states_for_event

View File

@ -96,9 +96,18 @@ module AASM
@aasm_current_state = state
end
def aasm_state_object_for_state(name)
self.class.aasm_states.find {|s| s == name}
end
def aasm_fire_event(name, persist)
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
new_state = self.class.aasm_events[name].fire(self)
unless new_state.nil?
aasm_state_object_for_state(new_state).call_action(:enter, self)
if self.respond_to?(:aasm_event_fired)
self.aasm_event_fired(self.aasm_current_state, new_state)
end

View File

@ -3,8 +3,8 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
class Foo
include AASM
aasm_initial_state :open
aasm_state :open
aasm_state :closed
aasm_state :open, :exit => :exit
aasm_state :closed, :enter => :enter
aasm_event :close, :success => :success_callback do
transitions :to => :closed, :from => [:open]
@ -20,6 +20,11 @@ class Foo
def success_callback
end
def enter
end
def exit
end
end
class Bar
@ -231,4 +236,18 @@ describe AASM, '- event callbacks' do
end
end
describe AASM, '- state actions' do
it "should call enter when entering state" do
foo = Foo.new
foo.should_receive(:enter)
foo.close
end
it "should call exit when exiting state" do
foo = Foo.new
foo.should_receive(:exit)
foo.close
end
end