diff --git a/spec/models/callbacks/basic.rb b/spec/models/callbacks/basic.rb index 2cadac1..54c68a3 100644 --- a/spec/models/callbacks/basic.rb +++ b/spec/models/callbacks/basic.rb @@ -35,7 +35,7 @@ module Callbacks :after_exit => :after_exit_closed event :close, :before => :before_event, :after => :after_event, :guard => :event_guard do - transitions :to => :closed, :from => [:open], :guard => :transition_guard, :after => :after_transition + transitions :to => :closed, :from => [:open], :guard => :transition_guard, :after => :after_transition, :success => :success_transition end event :open, :before => :before_event, :after => :after_event do @@ -68,6 +68,7 @@ module Callbacks def transition_guard; log('transition_guard'); !@fail_transition_guard; end def after_transition; log('after_transition'); end + def success_transition; log('transition_success'); end def before_event; log('before_event'); end def after_event; log('after_event'); end diff --git a/spec/models/callbacks/guard_within_block.rb b/spec/models/callbacks/guard_within_block.rb index 0556485..817a1f2 100644 --- a/spec/models/callbacks/guard_within_block.rb +++ b/spec/models/callbacks/guard_within_block.rb @@ -26,7 +26,7 @@ module Callbacks :after_exit => :after_exit_closed event :close, :before => :before, :after => :after, :guard => :event_guard do - transitions :to => :closed, :from => [:open], :after => :transitioning do + transitions :to => :closed, :from => [:open], :after => :transitioning, :success => :success_transition do guard do transition_guard end @@ -59,6 +59,7 @@ module Callbacks def event_guard; log('event_guard'); !@fail_event_guard; end def transition_guard; log('transition_guard'); !@fail_transition_guard; end def transitioning; log('transitioning'); end + def success_transition; log('success transition'); end def before; log('before'); end def after; log('after'); end diff --git a/spec/models/callbacks/multiple_transitions_transition_guard.rb b/spec/models/callbacks/multiple_transitions_transition_guard.rb index 1d67510..5fe1e96 100644 --- a/spec/models/callbacks/multiple_transitions_transition_guard.rb +++ b/spec/models/callbacks/multiple_transitions_transition_guard.rb @@ -28,7 +28,7 @@ module Callbacks state :failed event :close, :before => :before, :after => :after, :guard => :event_guard do - transitions :to => :closed, :from => [:open], :guard => :transition_guard, :after => :transitioning + transitions :to => :closed, :from => [:open], :guard => :transition_guard, :after => :transitioning, :success => :success_transition transitions :to => :failed, :from => [:open] end @@ -58,6 +58,7 @@ module Callbacks def event_guard; log('event_guard'); !@fail_event_guard; end def transition_guard; log('transition_guard'); !@fail_transition_guard; end def transitioning; log('transitioning'); end + def success_transition; log('transition success'); end def before; log('before'); end def after; log('after'); end diff --git a/spec/models/callbacks/with_args.rb b/spec/models/callbacks/with_args.rb index b348666..13c4c53 100644 --- a/spec/models/callbacks/with_args.rb +++ b/spec/models/callbacks/with_args.rb @@ -29,7 +29,7 @@ module Callbacks :after_exit => :after_exit_closed event :close, :before => :before, :after => :after do - transitions :to => :closed, :from => [:open], :after => :transition_proc + transitions :to => :closed, :from => [:open], :after => :transition_proc, :success => :transition_success_proc end event :open, :before => :before, :after => :after do @@ -57,5 +57,6 @@ module Callbacks def before(arg1, *args); log("before(#{arg1.inspect},#{args.map(&:inspect).join(',')})"); end def transition_proc(arg1, arg2); log("transition_proc(#{arg1.inspect},#{arg2.inspect})"); end def after(*args); log("after(#{args.map(&:inspect).join(',')})"); end + def transition_success_proc(*args); log("transition_success(#{args.map(&:inspect).join(',')})"); end end end diff --git a/spec/models/callbacks/with_state_args.rb b/spec/models/callbacks/with_state_args.rb index 2f259ae..e30957d 100644 --- a/spec/models/callbacks/with_state_args.rb +++ b/spec/models/callbacks/with_state_args.rb @@ -9,8 +9,8 @@ module Callbacks state :out_to_lunch event :close, :before => :before_method, :after => :after_method do - transitions :to => :closed, :from => [:open], :after => :transition_method - transitions :to => :out_to_lunch, :from => [:open], :after => :transition_method2 + transitions :to => :closed, :from => [:open], :after => :transition_method, :success => :success_method + transitions :to => :out_to_lunch, :from => [:open], :after => :transition_method2, :success => :success_method2 end end @@ -22,5 +22,9 @@ module Callbacks def transition_method2(arg); end + def success_method(arg); end + + def success_method2(arg); end + end end diff --git a/spec/models/parametrised_event.rb b/spec/models/parametrised_event.rb index 3a30d1c..184a9c2 100644 --- a/spec/models/parametrised_event.rb +++ b/spec/models/parametrised_event.rb @@ -12,18 +12,24 @@ class ParametrisedEvent end event :dress do - transitions :from => :sleeping, :to => :working, :after => :wear_clothes - transitions :from => :showering, :to => [:working, :dating], :after => Proc.new { |*args| wear_clothes(*args) } - transitions :from => :showering, :to => :prettying_up, :after => [:condition_hair, :fix_hair] + transitions :from => :sleeping, :to => :working, :after => :wear_clothes, :success => :wear_makeup + transitions :from => :showering, :to => [:working, :dating], :after => Proc.new { |*args| wear_clothes(*args) }, :success => proc { |*args| wear_makeup(*args) } + transitions :from => :showering, :to => :prettying_up, :after => [:condition_hair, :fix_hair], :success => [:touch_up_hair] end end def wear_clothes(shirt_color, trouser_type) end + def wear_makeup(makeup, moisturizer) + end + def condition_hair end def fix_hair end + + def touch_up_hair + end end diff --git a/spec/unit/callbacks_spec.rb b/spec/unit/callbacks_spec.rb index 9d60e7d..08b6851 100644 --- a/spec/unit/callbacks_spec.rb +++ b/spec/unit/callbacks_spec.rb @@ -13,15 +13,16 @@ describe 'callbacks for the new DSL' do expect(callback).to receive(:before_event).once.ordered expect(callback).to receive(:event_guard).once.ordered.and_return(true) expect(callback).to receive(:transition_guard).once.ordered.and_return(true) - expect(callback).to receive(:before_exit_open).once.ordered # these should be before the state changes + expect(callback).to receive(:before_exit_open).once.ordered # these should be before the state changes expect(callback).to receive(:exit_open).once.ordered # expect(callback).to receive(:event_guard).once.ordered.and_return(true) # expect(callback).to receive(:transition_guard).once.ordered.and_return(true) expect(callback).to receive(:after_transition).once.ordered expect(callback).to receive(:before_enter_closed).once.ordered expect(callback).to receive(:enter_closed).once.ordered - expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes - expect(callback).to receive(:after_exit_open).once.ordered # these should be after the state changes + expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes + expect(callback).to receive(:success_transition).once.ordered.and_return(true) # these should be after the state changes + expect(callback).to receive(:after_exit_open).once.ordered expect(callback).to receive(:after_enter_closed).once.ordered expect(callback).to receive(:after_event).once.ordered end @@ -43,6 +44,7 @@ describe 'callbacks for the new DSL' do expect(callback).to_not receive(:before_enter_closed) expect(callback).to_not receive(:enter_closed) expect(callback).to_not receive(:aasm_write_state) + expect(callback).to_not receive(:success_transition) expect(callback).to_not receive(:after_exit_open) expect(callback).to_not receive(:after_enter_closed) expect(callback).to_not receive(:after_event) @@ -80,6 +82,7 @@ describe 'callbacks for the new DSL' do expect(callback).to_not receive(:before_enter_closed) expect(callback).to_not receive(:enter_closed) expect(callback).to_not receive(:aasm_write_state) + expect(callback).to_not receive(:success_transition) expect(callback).to_not receive(:after_exit_open) expect(callback).to_not receive(:after_enter_closed) expect(callback).to_not receive(:after_event) @@ -107,6 +110,7 @@ describe 'callbacks for the new DSL' do expect(callback).to receive(:after).once.ordered expect(callback).to_not receive(:transitioning) + expect(callback).to_not receive(:success_transition) expect(callback).to_not receive(:before_enter_closed) expect(callback).to_not receive(:enter_closed) expect(callback).to_not receive(:after_enter_closed) @@ -129,6 +133,7 @@ describe 'callbacks for the new DSL' do expect(callback).to_not receive(:before_enter_closed) expect(callback).to_not receive(:enter_closed) expect(callback).to_not receive(:aasm_write_state) + expect(callback).to_not receive(:success_transition) expect(callback).to_not receive(:after_exit_open) expect(callback).to_not receive(:after_enter_closed) expect(callback).to_not receive(:after) @@ -145,14 +150,16 @@ describe 'callbacks for the new DSL' do cb.reset_data cb.close!(:arg1, :arg2) - expect(cb.data).to eql 'before(:arg1,:arg2) before_exit_open(:arg1,:arg2) transition_proc(:arg1,:arg2) before_enter_closed(:arg1,:arg2) aasm_write_state after_exit_open(:arg1,:arg2) after_enter_closed(:arg1,:arg2) after(:arg1,:arg2)' + expect(cb.data).to eql 'before(:arg1,:arg2) before_exit_open(:arg1,:arg2) transition_proc(:arg1,:arg2) before_enter_closed(:arg1,:arg2) aasm_write_state transition_success(:arg1,:arg2) after_exit_open(:arg1,:arg2) after_enter_closed(:arg1,:arg2) after(:arg1,:arg2)' end it "should call the callbacks given the to-state as argument" do cb = Callbacks::WithStateArg.new expect(cb).to receive(:before_method).with(:arg1).once.ordered expect(cb).to receive(:transition_method).never + expect(cb).to receive(:success_method).never expect(cb).to receive(:transition_method2).with(:arg1).once.ordered + expect(cb).to receive(:success_method2).with(:arg1).once.ordered expect(cb).to receive(:after_method).with(:arg1).once.ordered cb.close!(:out_to_lunch, :arg1) @@ -160,6 +167,7 @@ describe 'callbacks for the new DSL' do some_object = double('some object') expect(cb).to receive(:before_method).with(some_object).once.ordered expect(cb).to receive(:transition_method2).with(some_object).once.ordered + expect(cb).to receive(:success_method2).with(some_object).once.ordered expect(cb).to receive(:after_method).with(some_object).once.ordered cb.close!(:out_to_lunch, some_object) end @@ -169,6 +177,8 @@ describe 'callbacks for the new DSL' do expect(cb).to receive(:before_method).with(:arg1).once.ordered expect(cb).to receive(:transition_method).with(:arg1).once.ordered expect(cb).to receive(:transition_method).never + expect(cb).to receive(:success_method).with(:arg1).once.ordered + expect(cb).to receive(:success_method).never expect(cb).to receive(:after_method).with(:arg1).once.ordered cb.close!(:arg1) @@ -177,6 +187,8 @@ describe 'callbacks for the new DSL' do expect(cb).to receive(:before_method).with(some_object).once.ordered expect(cb).to receive(:transition_method).with(some_object).once.ordered expect(cb).to receive(:transition_method).never + expect(cb).to receive(:success_method).with(some_object).once.ordered + expect(cb).to receive(:success_method).never expect(cb).to receive(:after_method).with(some_object).once.ordered cb.close!(some_object) end @@ -192,7 +204,7 @@ describe 'event callbacks' do aasm do event :safe_close, :success => :success_callback, :error => :error_callback do - transitions :to => :closed, :from => [:open] + transitions :to => :closed, :from => [:open], :success => :transition_success_callback end end end diff --git a/spec/unit/event_spec.rb b/spec/unit/event_spec.rb index fb0bc24..2ab8c55 100644 --- a/spec/unit/event_spec.rb +++ b/spec/unit/event_spec.rb @@ -5,7 +5,7 @@ describe 'adding an event' do AASM::Core::Event.new(:close_order, {:success => :success_callback}) do before :before_callback after :after_callback - transitions :to => :closed, :from => [:open, :received] + transitions :to => :closed, :from => [:open, :received], success: [:transition_success_callback] end end @@ -303,6 +303,24 @@ describe 'parametrised events' do expect(pe).to receive(:fix_hair) pe.dress!(:prettying_up) end + + it 'should call :success transition method with args' do + pe.wakeup!(:showering) + expect(pe).to receive(:wear_makeup).with('foundation', 'SPF') + pe.dress!(:working, 'foundation', 'SPF') + end + + it 'should call :success transition proc' do + pe.wakeup!(:showering) + expect(pe).to receive(:wear_makeup).with('purple', 'slacks') + pe.dress!(:dating, 'purple', 'slacks') + end + + it 'should call :success transition with an array of methods' do + pe.wakeup!(:showering) + expect(pe).to receive(:touch_up_hair) + pe.dress!(:prettying_up) + end end describe 'event firing without persistence' do diff --git a/spec/unit/transition_spec.rb b/spec/unit/transition_spec.rb index 802f449..8d13fa9 100644 --- a/spec/unit/transition_spec.rb +++ b/spec/unit/transition_spec.rb @@ -336,19 +336,4 @@ describe AASM::Core::Transition, '- when invoking the transition :success method 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