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

accept params for may_event? and pass them to guard

This commit is contained in:
Pavel Gabriel 2012-09-18 12:21:20 +03:00
parent 4ad285795a
commit d5f6965bae
3 changed files with 14 additions and 3 deletions

View file

@ -12,14 +12,14 @@ module AASM
# a neutered version of fire - it doesn't actually fire the event, it just
# executes the transition guards to determine if a transition is even
# an option given current conditions.
def may_fire?(obj, to_state=nil)
def may_fire?(obj, to_state=nil, *args)
transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
return false if transitions.size == 0
result = false
transitions.each do |transition|
next if to_state and !Array(transition.to).include?(to_state)
if transition.perform(obj)
if transition.perform(obj, *args)
result = true
break
end

View file

@ -111,6 +111,7 @@ class AuthMachine
event :unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| u.has_activated? }
transitions :from => :suspended, :to => :active, :guard => :if_polite?
transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| u.has_activation_code? }
transitions :from => :suspended, :to => :passive
end
@ -150,6 +151,10 @@ class AuthMachine
def has_activation_code?
!!@activation_code
end
def if_polite?(phrase = nil)
phrase == :please
end
end
class ThisNameBetterNotBeInUse

View file

@ -28,6 +28,12 @@ describe 'AuthMachine when being unsuspended' do
@auth.suspend!
@auth.may_unsuspend?(:active).should_not be_true
end
it 'should be able to be unsuspended into active if polite' do
@auth = AuthMachine.new
@auth.suspend!
@auth.may_unsuspend?(:active, :please).should be_true
end
it 'should not be able to be unpassified' do
@auth = AuthMachine.new