From 7f9be4a97ed912291088e5502bb600319994b37b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20Bo=CC=88ttger?= Date: Sun, 11 May 2014 23:19:06 +0100 Subject: [PATCH] allow providing any argument for :on_transition callback (fix for issue #126) --- lib/aasm/event.rb | 3 +-- spec/unit/callbacks_spec.rb | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/aasm/event.rb b/lib/aasm/event.rb index 4e9efaf..80fd5f6 100644 --- a/lib/aasm/event.rb +++ b/lib/aasm/event.rb @@ -103,8 +103,7 @@ module AASM # If to_state is not nil it either contains a potential # to_state or an arg unless to_state == nil - if to_state.respond_to?(:to_sym) && - !transitions.map(&:to).flatten.include?(to_state.to_sym) + if !to_state.respond_to?(:to_sym) || !transitions.map(&:to).flatten.include?(to_state.to_sym) args.unshift(to_state) to_state = nil end diff --git a/spec/unit/callbacks_spec.rb b/spec/unit/callbacks_spec.rb index 6d807a5..646db24 100644 --- a/spec/unit/callbacks_spec.rb +++ b/spec/unit/callbacks_spec.rb @@ -33,7 +33,23 @@ describe 'callbacks for the new DSL' do cb.close!(:arg1, :arg2) end - it "should call the proper methods given a to state as the first arg" do + it "should call the callbacks given the to-state as argument" do + cb = CallbackWithStateArg.new + cb.should_receive(:before_method).with(:arg1).once.ordered + cb.should_receive(:transition_method).never + cb.should_receive(:transition_method2).with(:arg1).once.ordered + cb.should_receive(:after_method).with(:arg1).once.ordered + cb.close!(:out_to_lunch, :arg1) + + cb = CallbackWithStateArg.new + some_object = double('some object') + cb.should_receive(:before_method).with(some_object).once.ordered + cb.should_receive(:transition_method2).with(some_object).once.ordered + cb.should_receive(:after_method).with(some_object).once.ordered + cb.close!(:out_to_lunch, some_object) + end + + it "should call the proper methods just with arguments" do cb = CallbackWithStateArg.new cb.should_receive(:before_method).with(:arg1).once.ordered cb.should_receive(:transition_method).with(:arg1).once.ordered @@ -42,11 +58,12 @@ describe 'callbacks for the new DSL' do cb.close!(:arg1) cb = CallbackWithStateArg.new - cb.should_receive(:before_method).with(:arg1).once.ordered + some_object = double('some object') + cb.should_receive(:before_method).with(some_object).once.ordered + cb.should_receive(:transition_method).with(some_object).once.ordered cb.should_receive(:transition_method).never - cb.should_receive(:transition_method2).with(:arg1).once.ordered - cb.should_receive(:after_method).with(:arg1).once.ordered - cb.close!(:out_to_lunch, :arg1) + cb.should_receive(:after_method).with(some_object).once.ordered + cb.close!(some_object) end end