mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Add success callback for Transition
This commit is contained in:
parent
44a17aa535
commit
22de92318d
2 changed files with 77 additions and 2 deletions
|
@ -6,7 +6,7 @@ module AASM::Core
|
|||
alias_method :options, :opts
|
||||
|
||||
def initialize(opts, &block)
|
||||
add_options_from_dsl(opts, [:on_transition, :guard, :after], &block) if block
|
||||
add_options_from_dsl(opts, [:on_transition, :guard, :after, :success], &block) if block
|
||||
|
||||
@from = opts[:from]
|
||||
@to = opts[:to]
|
||||
|
@ -20,6 +20,9 @@ module AASM::Core
|
|||
@after = Array(opts[:after])
|
||||
@after = @after[0] if @after.size == 1
|
||||
|
||||
@success = Array(opts[:success])
|
||||
@success = @success[0] if @success.size == 1
|
||||
|
||||
@opts = opts
|
||||
end
|
||||
|
||||
|
|
|
@ -79,15 +79,17 @@ describe AASM::Core::Transition do
|
|||
expect(st.opts[:after]).to eql [:after_callback]
|
||||
end
|
||||
|
||||
it 'should set after and guard from dsl' do
|
||||
it 'should set after, guard and success from dsl' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::Core::Transition.new(opts) do
|
||||
guard :gg
|
||||
after :after_callback
|
||||
success :after_persist
|
||||
end
|
||||
|
||||
expect(st.opts[:guard]).to eql ['g', :gg]
|
||||
expect(st.opts[:after]).to eql [:after_callback] # TODO fix this bad code coupling
|
||||
expect(st.opts[:success]).to eql [:after_persist] # TODO fix this bad code coupling
|
||||
end
|
||||
|
||||
it 'should pass equality check if from and to are the same' do
|
||||
|
@ -280,3 +282,73 @@ describe AASM::Core::Transition, '- when executing the transition with an :after
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
describe AASM::Core::Transition, '- when invoking the transition :success method call' do
|
||||
it 'should accept a String for the method name' do
|
||||
opts = {:from => 'foo', :to => 'bar', :success => 'test'}
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
expect(obj).to receive(:test)
|
||||
|
||||
st.invoke_success_callbacks(obj, args)
|
||||
end
|
||||
|
||||
it 'should accept a Symbol for the method name' do
|
||||
opts = {:from => 'foo', :to => 'bar', :success => :test}
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
expect(obj).to receive(:test)
|
||||
|
||||
st.invoke_success_callbacks(obj, args)
|
||||
end
|
||||
|
||||
it 'should pass args if the target method accepts them' do
|
||||
opts = {:from => 'foo', :to => 'bar', :success => :test}
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
def obj.test(args)
|
||||
"arg1: #{args[:arg1]} arg2: #{args[:arg2]}"
|
||||
end
|
||||
|
||||
return_value = st.invoke_success_callbacks(obj, args)
|
||||
|
||||
expect(return_value).to eq('arg1: 1 arg2: 2')
|
||||
end
|
||||
|
||||
it 'should NOT pass args if the target method does NOT accept them' do
|
||||
opts = {:from => 'foo', :to => 'bar', :success => :test}
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
def obj.test
|
||||
'success'
|
||||
end
|
||||
|
||||
return_value = st.invoke_success_callbacks(obj, args)
|
||||
|
||||
expect(return_value).to eq('success')
|
||||
end
|
||||
|
||||
it 'should allow accessing the from_state and the to_state' do
|
||||
opts = {:from => 'foo', :to => 'bar', :success => :test}
|
||||
transition = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => AASM::InstanceBase.new('object'))
|
||||
|
||||
def obj.test(args)
|
||||
"from: #{aasm.from_state} to: #{aasm.to_state}"
|
||||
end
|
||||
|
||||
return_value = transition.invoke_success_callbacks(obj, args)
|
||||
|
||||
expect(return_value).to eq('from: foo to: bar')
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue