Skip to content

Commit

Permalink
Added ability to pass an array of methods to on_transition callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jacqui committed Jan 29, 2009
1 parent 558fd12 commit 1ab8287
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/state_transition.rb
Expand Up @@ -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
Expand Down
32 changes: 24 additions & 8 deletions spec/unit/aasm_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand All @@ -361,18 +362,25 @@ 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


describe ChetanPatil do
it 'should transition to specified next state (sleeping to showering)' do
cp = ChetanPatil.new
cp.wakeup! :showering

cp.aasm_current_state.should == :showering
end

Expand Down Expand Up @@ -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

0 comments on commit 1ab8287

Please sign in to comment.