From 1ab82879696b889eea9d91b489a87541ebb5c83d Mon Sep 17 00:00:00 2001 From: Jacqui Maher Date: Thu, 29 Jan 2009 12:19:48 -0500 Subject: [PATCH] Added ability to pass an array of methods to on_transition callback --- lib/state_transition.rb | 4 +++- spec/unit/aasm_spec.rb | 32 ++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/state_transition.rb b/lib/state_transition.rb index f82f5d48..c823f020 100644 --- a/lib/state_transition.rb +++ b/lib/state_transition.rb @@ -18,11 +18,13 @@ def perform(obj) true end end - + def execute(obj, *args) case @on_transition when Symbol, String obj.send(@on_transition, *args) + when Array + @on_transition.each{|m| obj.send(m, *args) } when Proc @on_transition.call(obj, *args) end diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index 30cfe87d..7cb99a88 100644 --- a/spec/unit/aasm_spec.rb +++ b/spec/unit/aasm_spec.rb @@ -54,11 +54,11 @@ class Baz < Bar it 'should define a class level aasm_event() method on its including class' do Foo.should respond_to(:aasm_event) end - + it 'should define a class level aasm_states() method on its including class' do Foo.should respond_to(:aasm_states) end - + it 'should define a class level aasm_states_for_select() method on its including class' do Foo.should respond_to(:aasm_states_for_select) end @@ -157,7 +157,7 @@ class Baz < Bar it 'should attempt to persist if aasm_write_state is defined' do foo = Foo.new - + def foo.aasm_write_state end @@ -168,7 +168,7 @@ def foo.aasm_write_state it 'should return true if aasm_write_state is defined and returns true' do foo = Foo.new - + def foo.aasm_write_state(state) true end @@ -178,7 +178,7 @@ def foo.aasm_write_state(state) it 'should return false if aasm_write_state is defined and returns false' do foo = Foo.new - + def foo.aasm_write_state(state) false end @@ -188,7 +188,7 @@ def foo.aasm_write_state(state) it "should not update the aasm_current_state if the write fails" do foo = Foo.new - + def foo.aasm_write_state false end @@ -217,7 +217,7 @@ def foo.aasm_write_state it 'should attempt to persist if aasm_write_state is defined' do foo = Foo.new - + def foo.aasm_write_state end @@ -353,6 +353,7 @@ class ChetanPatil aasm_state :showering aasm_state :working aasm_state :dating + aasm_state :prettying_up aasm_event :wakeup do transitions :from => :sleeping, :to => [:showering, :working] @@ -361,10 +362,17 @@ class ChetanPatil aasm_event :dress do transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) } + transitions :from => :showering, :to => :prettying_up, :on_transition => [:condition_hair, :fix_hair] end def wear_clothes(shirt_color, trouser_type) end + + def condition_hair + end + + def fix_hair + end end @@ -372,7 +380,7 @@ def wear_clothes(shirt_color, trouser_type) it 'should transition to specified next state (sleeping to showering)' do cp = ChetanPatil.new cp.wakeup! :showering - + cp.aasm_current_state.should == :showering end @@ -412,4 +420,12 @@ def wear_clothes(shirt_color, trouser_type) cp.should_receive(:wear_clothes).with('purple', 'slacks') cp.dress!(:dating, 'purple', 'slacks') end + + it 'should call on_transition with an array of methods' do + cp = ChetanPatil.new + cp.wakeup! :showering + cp.should_receive(:condition_hair) + cp.should_receive(:fix_hair) + cp.dress!(:prettying_up) + end end