1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

Add option to use Class that responds to call for after transitions

This commit is contained in:
Ronnie Miller 2016-02-01 20:22:43 -08:00
parent ddda181cd5
commit cd8a2ef12d
2 changed files with 75 additions and 0 deletions

View file

@ -61,6 +61,18 @@ module AASM::Core
result = (code.parameters.size == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code)) result = (code.parameters.size == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code))
failures << code.source_location.join('#') unless result failures << code.source_location.join('#') unless result
result result
when Class
arity = code.instance_method(:initialize).arity
if arity == 0
instance = code.new
elsif arity == 1
instance = code.new(record)
else
instance = code.new(record, *args)
end
result = instance.call
failures << instance.method(:call).source_location.join('#') unless result
result
when Array when Array
if options[:guard] if options[:guard]
# invoke guard callbacks # invoke guard callbacks

View file

@ -300,3 +300,66 @@ describe AASM::Core::Transition, '- when executing the transition with an :after
end end
end end
describe AASM::Core::Transition, '- when executing the transition with a Class' do
let(:state_machine) { AASM::StateMachine.new(:name) }
let(:event) { AASM::Core::Event.new(:event, state_machine) }
class AfterTransitionClass
def initialize(record)
@record = record
end
def call
"from: #{@record.aasm.from_state} to: #{@record.aasm.to_state}"
end
end
class AfterTransitionClassWithArgs
def initialize(record, args)
@record = record
@args = args
end
def call
"arg1: #{@args[:arg1]}, arg2: #{@args[:arg2]}"
end
end
class AfterTransitionClassWithoutArgs
def call
'success'
end
end
it 'passes the record to the initialize method on the class to give access to the from_state and to_state' do
opts = {:from => 'foo', :to => 'bar', :after => AfterTransitionClass}
transition = AASM::Core::Transition.new(event, opts)
obj = double('object', :aasm => AASM::InstanceBase.new('object'))
return_value = transition.execute(obj)
expect(return_value).to eq('from: foo to: bar')
end
it 'should pass args to the initialize method on the class if it accepts them' do
opts = {:from => 'foo', :to => 'bar', :after => AfterTransitionClassWithArgs}
st = AASM::Core::Transition.new(event, opts)
args = {:arg1 => '1', :arg2 => '2'}
obj = double('object', :aasm => 'aasm')
return_value = st.execute(obj, args)
expect(return_value).to eq('arg1: 1, arg2: 2')
end
it 'should NOT pass args if the call method of the class if it does NOT accept them' do
opts = {:from => 'foo', :to => 'bar', :after => AfterTransitionClassWithoutArgs}
st = AASM::Core::Transition.new(event, opts)
obj = double('object', :aasm => 'aasm')
return_value = st.execute(obj)
expect(return_value).to eq('success')
end
end