mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Merge pull request #408 from udacity/disambiguate-transitions-with-same-to-and-from
Allow multiple transitions in a single event with the same to and from states
This commit is contained in:
commit
435826897f
3 changed files with 49 additions and 3 deletions
|
@ -128,12 +128,13 @@ module AASM::Core
|
||||||
|
|
||||||
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)
|
||||||
if (options.key?(:may_fire) && Array(transition.to).include?(options[:may_fire])) ||
|
if (options.key?(:may_fire) && transition.eql?(options[:may_fire])) ||
|
||||||
(!options.key?(:may_fire) && transition.allowed?(obj, *args))
|
(!options.key?(:may_fire) && transition.allowed?(obj, *args))
|
||||||
result = to_state || Array(transition.to).first
|
|
||||||
if options[:test_only]
|
if options[:test_only]
|
||||||
# result = true
|
result = transition
|
||||||
else
|
else
|
||||||
|
result = to_state || Array(transition.to).first
|
||||||
Array(transition.to).each {|to| @valid_transitions[to] = transition }
|
Array(transition.to).each {|to| @valid_transitions[to] = transition }
|
||||||
transition.execute(obj, *args)
|
transition.execute(obj, *args)
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
class MultipleTransitionsThatDifferOnlyByGuard
|
||||||
|
include AASM
|
||||||
|
|
||||||
|
attr_accessor :executed_second
|
||||||
|
|
||||||
|
aasm do
|
||||||
|
state :start, :initial => true
|
||||||
|
state :gone
|
||||||
|
|
||||||
|
event :go do
|
||||||
|
transitions :from => :start, :to => :gone, :guard => :returns_false, :after => :this_should_not_execute
|
||||||
|
transitions :from => :start, :to => :gone, :guard => :returns_true, :after => :this_should_execute
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def returns_false
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def returns_true
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def this_should_not_execute
|
||||||
|
raise RuntimeError, "This should not execute"
|
||||||
|
end
|
||||||
|
|
||||||
|
def this_should_execute
|
||||||
|
self.executed_second = true
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,14 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe "multiple transitions that differ only by guard" do
|
||||||
|
let(:job) { MultipleTransitionsThatDifferOnlyByGuard.new }
|
||||||
|
|
||||||
|
it 'does not follow the first transition if its guard fails' do
|
||||||
|
expect{job.go}.not_to raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'executes the second transition\'s callbacks' do
|
||||||
|
job.go
|
||||||
|
expect(job.executed_second).to be_truthy
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue