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

Use AASM::NO_VALUE as default value for to_state argument

This commit is contained in:
Huiming Teo 2017-07-26 21:05:43 +08:00 committed by Anil Kumar Maurya
parent 6d6bcf8471
commit 119775f0b1
2 changed files with 12 additions and 11 deletions

View file

@ -1,4 +1,6 @@
module AASM module AASM
# this is used internally as an argument default value to represent no value
NO_VALUE = :_aasm_no_value
# provide a state machine for the including class # provide a state machine for the including class
# make sure to load class methods as well # make sure to load class methods as well

View file

@ -40,11 +40,11 @@ module AASM::Core
# a neutered version of fire - it doesn't actually fire the event, it just # 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 # executes the transition guards to determine if a transition is even
# an option given current conditions. # an option given current conditions.
def may_fire?(obj, to_state=nil, *args) def may_fire?(obj, to_state=::AASM::NO_VALUE, *args)
_fire(obj, {:test_only => true}, to_state, *args) # true indicates test firing _fire(obj, {:test_only => true}, to_state, *args) # true indicates test firing
end end
def fire(obj, options={}, to_state=nil, *args) def fire(obj, options={}, to_state=::AASM::NO_VALUE, *args)
_fire(obj, options, to_state, *args) # false indicates this is not a test (fire!) _fire(obj, options, to_state, *args) # false indicates this is not a test (fire!)
end end
@ -121,21 +121,20 @@ module AASM::Core
definitions definitions
end end
def _fire(obj, options={}, to_state=nil, *args) def _fire(obj, options={}, to_state=::AASM::NO_VALUE, *args)
result = options[:test_only] ? false : nil result = options[:test_only] ? false : nil
transitions = @transitions.select { |t| t.from == obj.aasm(state_machine.name).current_state || t.from == nil} transitions = @transitions.select { |t| t.from == obj.aasm(state_machine.name).current_state || t.from == nil}
return result if transitions.size == 0 return result if transitions.size == 0
# If to_state is not nil it either contains a potential if to_state == ::AASM::NO_VALUE
# to_state or an arg to_state = nil
unless to_state == nil elsif to_state.respond_to?(:to_sym) && transitions.map(&:to).flatten.include?(to_state.to_sym)
if !to_state.respond_to?(:to_sym) || !transitions.map(&:to).flatten.include?(to_state.to_sym) # nop, to_state is a valid to-state
else
# to_state is an argument
args.unshift(to_state) args.unshift(to_state)
to_state = nil to_state = nil
end end
else
args.unshift(nil) if args.nil? || args.empty? # If single arg given which is nil, push it back to args
end
transitions.each do |transition| transitions.each do |transition|
next if to_state and !Array(transition.to).include?(to_state) next if to_state and !Array(transition.to).include?(to_state)