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

Ensure nil arg is forwarded to callbacks

fixes #404
This commit is contained in:
Yogesh Khater 2016-10-04 20:12:15 +05:30
parent 006fe3bf1d
commit c3b6454e8e
5 changed files with 26 additions and 1 deletions

View file

@ -122,6 +122,8 @@ module AASM::Core
args.unshift(to_state)
to_state = nil
end
else
args.unshift(nil) if args.blank? # If single arg given which is nil, push it back to args
end
transitions.each do |transition|

View file

@ -10,7 +10,7 @@ class GuardWithParams
def user_is_manager?(user)
ok = false
if user.has_role? :manager
if user && user.has_role?(:manager)
ok = true
end
return ok

View file

@ -11,6 +11,10 @@ class ParametrisedEvent
transitions :from => :sleeping, :to => [:showering, :working]
end
event :shower do
transitions :from => :sleeping, :to => :showering, :after => :wet_hair, :success => :wear_clothes
end
event :dress do
transitions :from => :sleeping, :to => :working, :after => :wear_clothes, :success => :wear_makeup
transitions :from => :showering, :to => [:working, :dating], :after => Proc.new { |*args| wear_clothes(*args) }, :success => proc { |*args| wear_makeup(*args) }
@ -24,6 +28,9 @@ class ParametrisedEvent
def wear_makeup(makeup, moisturizer)
end
def wet_hair(dryer)
end
def condition_hair
end

View file

@ -325,6 +325,12 @@ describe 'parametrised events' do
pe.dress!(:working, 'blue', 'jeans')
end
it 'should call :after transition method if arg is nil' do
dryer = nil
expect(pe).to receive(:wet_hair).with(dryer)
pe.shower!(dryer)
end
it 'should call :after transition proc' do
pe.wakeup!(:showering)
expect(pe).to receive(:wear_clothes).with('purple', 'slacks')
@ -344,6 +350,12 @@ describe 'parametrised events' do
pe.dress!(:working, 'foundation', 'SPF')
end
it 'should call :success transition method if arg is nil' do
shirt_color = nil
expect(pe).to receive(:wear_clothes).with(shirt_color)
pe.shower!(shirt_color)
end
it 'should call :success transition proc' do
pe.wakeup!(:showering)
expect(pe).to receive(:wear_makeup).with('purple', 'slacks')

View file

@ -7,4 +7,8 @@ describe "guards with params" do
it "list permitted states" do
expect(guard.aasm.states({:permitted => true}, user).map(&:name)).to eql [:reviewed]
end
it "list no states if user is blank" do
expect(guard.aasm.states({:permitted => true}, nil).map(&:name)).to eql []
end
end