Fix guard checking when from is not provided (#456)

* add specs
This commit is contained in:
Adam Szczombrowski 2017-04-27 12:22:54 +02:00 committed by Anil Kumar Maurya
parent ffc2bf7725
commit cf21c8b583
3 changed files with 29 additions and 1 deletions

View File

@ -96,7 +96,7 @@ module AASM::Core
@transitions << AASM::Core::Transition.new(self, attach_event_guards(definitions.merge(:from => s.to_sym)), &block)
end
# Create a transition if :to is specified without :from (transitions from ANY state)
if @transitions.empty? && definitions[:to]
if !definitions[:from] && definitions[:to]
@transitions << AASM::Core::Transition.new(self, attach_event_guards(definitions), &block)
end
end

View File

@ -0,0 +1,18 @@
class GuardianWithoutFromSpecified
include AASM
aasm do
state :alpha, :initial => true
state :beta
state :gamma
event :use_guards_where_the_first_fails do
transitions :to => :beta, :guard => :fail
transitions :to => :gamma, :guard => :succeed
end
end
def fail; false; end
def succeed; true; end
end

View File

@ -0,0 +1,10 @@
require 'spec_helper'
describe "transitions without from specified" do
let(:guardian) { GuardianWithoutFromSpecified.new }
it "allows the transitions if guard succeeds" do
expect { guardian.use_guards_where_the_first_fails! }.to_not raise_error
expect(guardian).to be_gamma
end
end