From 22de92318d0abbb8684b47e85299c78fa6f0f9a5 Mon Sep 17 00:00:00 2001 From: Bregadeesh Samapathy Date: Thu, 18 Jun 2015 16:59:40 +0800 Subject: [PATCH] Add success callback for Transition --- lib/aasm/core/transition.rb | 5 ++- spec/unit/transition_spec.rb | 74 +++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/lib/aasm/core/transition.rb b/lib/aasm/core/transition.rb index e4a80af..cbb11a2 100644 --- a/lib/aasm/core/transition.rb +++ b/lib/aasm/core/transition.rb @@ -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 diff --git a/spec/unit/transition_spec.rb b/spec/unit/transition_spec.rb index 6d9194c..802f449 100644 --- a/spec/unit/transition_spec.rb +++ b/spec/unit/transition_spec.rb @@ -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