From cf21c8b5834017a37dafec895d5e83ef242a609f Mon Sep 17 00:00:00 2001 From: Adam Szczombrowski Date: Thu, 27 Apr 2017 12:22:54 +0200 Subject: [PATCH] Fix guard checking when from is not provided (#456) * add specs --- lib/aasm/core/event.rb | 2 +- spec/models/guardian_without_from_specified.rb | 18 ++++++++++++++++++ spec/unit/guard_without_from_specified_spec.rb | 10 ++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 spec/models/guardian_without_from_specified.rb create mode 100644 spec/unit/guard_without_from_specified_spec.rb diff --git a/lib/aasm/core/event.rb b/lib/aasm/core/event.rb index 674122f..1e15110 100644 --- a/lib/aasm/core/event.rb +++ b/lib/aasm/core/event.rb @@ -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 diff --git a/spec/models/guardian_without_from_specified.rb b/spec/models/guardian_without_from_specified.rb new file mode 100644 index 0000000..f02bee3 --- /dev/null +++ b/spec/models/guardian_without_from_specified.rb @@ -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 diff --git a/spec/unit/guard_without_from_specified_spec.rb b/spec/unit/guard_without_from_specified_spec.rb new file mode 100644 index 0000000..cc59c8c --- /dev/null +++ b/spec/unit/guard_without_from_specified_spec.rb @@ -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