1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

Passing event arguments to the guard function

This commit is contained in:
Ravi Bhim 2011-03-21 20:01:45 -07:00
parent 0d8b9ab108
commit 7fe7e2ed57
3 changed files with 18 additions and 4 deletions

View file

@ -14,7 +14,7 @@ class AASM::SupportingClasses::Event
next_state = nil
transitions.each do |transition|
next if to_state and !Array(transition.to).include?(to_state)
if transition.perform(obj)
if transition.perform(obj, *args)
next_state = to_state || Array(transition.to).first
transition.execute(obj, *args)
break

View file

@ -7,12 +7,12 @@ class AASM::SupportingClasses::StateTransition
@opts = opts
end
def perform(obj)
def perform(obj, *args)
case @guard
when Symbol, String
obj.send(@guard)
obj.send(@guard, *args)
when Proc
@guard.call(obj)
@guard.call(obj, *args)
else
true
end

View file

@ -48,6 +48,20 @@ describe AASM::SupportingClasses::Event, 'when firing an event' do
event.fire(obj).should == :closed
end
it 'should call the guard with the params passed in' do
event = AASM::SupportingClasses::Event.new(:event) do
transitions :to => :closed, :from => [:open, :received], :guard => :guard_fn
end
obj = mock('object')
obj.stub!(:aasm_current_state).and_return(:open)
obj.should_receive(:guard_fn).with('arg1', 'arg2').and_return(true)
event.fire(obj, nil, 'arg1', 'arg2').should == :closed
end
end
describe AASM::SupportingClasses::Event, 'when executing the success callback' do