mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Modify State to prep for enter/entering/exit actions
This commit is contained in:
parent
0ee1ad986f
commit
30a8d15ef5
2 changed files with 43 additions and 52 deletions
27
lib/state.rb
27
lib/state.rb
|
@ -7,22 +7,23 @@ module AASM
|
||||||
@name, @options = name, options
|
@name, @options = name, options
|
||||||
end
|
end
|
||||||
|
|
||||||
def entering(record)
|
def ==(state)
|
||||||
enteract = @options[:enter]
|
if state.is_a? Symbol
|
||||||
record.send(:run_transition_action, enteract) if enteract
|
name == state
|
||||||
end
|
else
|
||||||
|
name == state.name
|
||||||
def entered(record)
|
|
||||||
afteractions = @options[:after]
|
|
||||||
return unless afteractions
|
|
||||||
Array(afteractions).each do |afteract|
|
|
||||||
record.send(:run_transition_action, afteract)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def exited(record)
|
def call_action(action, record)
|
||||||
exitact = @options[:exit]
|
action = @options[action]
|
||||||
record.send(:run_transition_action, exitact) if exitact
|
case action
|
||||||
|
when Symbol, String
|
||||||
|
record.send(action)
|
||||||
|
when Proc
|
||||||
|
action.call(record)
|
||||||
|
end
|
||||||
|
# symbol, proc, lambda
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,68 +5,58 @@ describe AASM::SupportingClasses::State do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@name = :astate
|
@name = :astate
|
||||||
@options = { :crazy_custom_key => 'key' }
|
@options = { :crazy_custom_key => 'key' }
|
||||||
@record = mock('record')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_state
|
def new_state(options={})
|
||||||
@state = AASM::SupportingClasses::State.new(@name, @options)
|
AASM::SupportingClasses::State.new(@name, @options.merge(options))
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should set the name' do
|
it 'should set the name' do
|
||||||
new_state
|
state = new_state
|
||||||
|
|
||||||
@state.name.should == :astate
|
state.name.should == :astate
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should set the options and expose them as options' do
|
it 'should set the options and expose them as options' do
|
||||||
new_state
|
state = new_state
|
||||||
|
|
||||||
@state.options.should == @options
|
state.options.should == @options
|
||||||
end
|
end
|
||||||
|
|
||||||
it '#entering should not run_transition_action if :enter option is not passed' do
|
it 'should be equal to a symbol of the same name' do
|
||||||
new_state
|
state = new_state
|
||||||
@record.should_not_receive(:run_transition_action)
|
|
||||||
|
|
||||||
@state.entering(@record)
|
state.should == :astate
|
||||||
end
|
end
|
||||||
|
|
||||||
it '#entered should not run_transition_action if :after option is not passed' do
|
it 'should be equal to a State of the same name' do
|
||||||
new_state
|
new_state.should == new_state
|
||||||
@record.should_not_receive(:run_transition_action)
|
|
||||||
|
|
||||||
@state.entered(@record)
|
|
||||||
end
|
|
||||||
|
|
||||||
it '#exited should not run_transition_action if :exit option is not passed' do
|
|
||||||
new_state
|
|
||||||
@record.should_not_receive(:run_transition_action)
|
|
||||||
|
|
||||||
@state.exited(@record)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it '#entering should run_transition_action when :enter option is passed' do
|
it 'should send a message to the record for an action if the action is present as a symbol' do
|
||||||
@options[:enter] = true
|
state = new_state(:entering => :foo)
|
||||||
new_state
|
|
||||||
@record.should_receive(:run_transition_action).with(true)
|
|
||||||
|
|
||||||
@state.entering(@record)
|
record = mock('record')
|
||||||
|
record.should_receive(:foo)
|
||||||
|
|
||||||
|
state.call_action(:entering, record)
|
||||||
end
|
end
|
||||||
|
|
||||||
it '#entered should run_transition_action for each option when :after option is passed' do
|
it 'should send a message to the record for an action if the action is present as a string' do
|
||||||
@options[:after] = ['a', 'b']
|
state = new_state(:entering => 'foo')
|
||||||
new_state
|
|
||||||
@record.should_receive(:run_transition_action).once.with('a')
|
|
||||||
@record.should_receive(:run_transition_action).once.with('b')
|
|
||||||
|
|
||||||
@state.entered(@record)
|
record = mock('record')
|
||||||
|
record.should_receive(:foo)
|
||||||
|
|
||||||
|
state.call_action(:entering, record)
|
||||||
end
|
end
|
||||||
|
|
||||||
it '#exited should run_transition_action when :exit option is passed' do
|
it 'should call a proc, passing in the record for an action if the action is present' do
|
||||||
@options[:exit] = true
|
state = new_state(:entering => Proc.new {|r| r.foobar})
|
||||||
new_state
|
|
||||||
@record.should_receive(:run_transition_action).with(true)
|
|
||||||
|
|
||||||
@state.exited(@record)
|
record = mock('record')
|
||||||
|
record.should_receive(:foobar)
|
||||||
|
|
||||||
|
state.call_action(:entering, record)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue