Skip to content

Commit

Permalink
Passing event arguments to the guard function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravi Bhim committed Mar 22, 2011
1 parent 0d8b9ab commit 7fe7e2e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/aasm/event.rb
Expand Up @@ -14,7 +14,7 @@ def fire(obj, to_state=nil, *args)
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
Expand Down
6 changes: 3 additions & 3 deletions lib/aasm/state_transition.rb
Expand Up @@ -7,12 +7,12 @@ def initialize(opts)
@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
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/event_spec.rb
Expand Up @@ -48,6 +48,20 @@ def new_event

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

0 comments on commit 7fe7e2e

Please sign in to comment.