Support enter and exit actions on states
This commit is contained in:
parent
2bb30ee84b
commit
3ccbeaca88
|
@ -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
1
TODO
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue