diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 2d95690..a1b2521 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -110,10 +110,10 @@ private if new_state_name = event.fire(self, {:may_fire => may_fire_to}, *args) aasm_fired(state_machine_name, event, old_state, new_state_name, options, *args, &block) else - aasm_failed(state_machine_name, event_name, old_state) + aasm_failed(state_machine_name, event_name, old_state, event.failed_callbacks) end else - aasm_failed(state_machine_name, event_name, old_state) + aasm_failed(state_machine_name, event_name, old_state, event.failed_callbacks) end rescue StandardError => e event.fire_callbacks(:error, self, e, *process_args(event, aasm(state_machine_name).current_state, *args)) || @@ -172,13 +172,13 @@ private persist_successful end - def aasm_failed(state_machine_name, event_name, old_state) + def aasm_failed(state_machine_name, event_name, old_state, failures = []) if self.respond_to?(:aasm_event_failed) self.aasm_event_failed(event_name, old_state.name) end if AASM::StateMachine[self.class][state_machine_name].config.whiny_transitions - raise AASM::InvalidTransition.new(self, event_name, state_machine_name) + raise AASM::InvalidTransition.new(self, event_name, state_machine_name, failures) else false end diff --git a/lib/aasm/core/event.rb b/lib/aasm/core/event.rb index c2d2cc1..e54d32a 100644 --- a/lib/aasm/core/event.rb +++ b/lib/aasm/core/event.rb @@ -85,6 +85,10 @@ module AASM::Core @transitions end + def failed_callbacks + transitions.flat_map(&:failures) + end + private def attach_event_guards(definitions) diff --git a/lib/aasm/core/transition.rb b/lib/aasm/core/transition.rb index c31b2d2..87bd0e0 100644 --- a/lib/aasm/core/transition.rb +++ b/lib/aasm/core/transition.rb @@ -2,7 +2,7 @@ module AASM::Core class Transition include DslHelper - attr_reader :from, :to, :event, :opts + attr_reader :from, :to, :event, :opts, :failures alias_method :options, :opts def initialize(event, opts, &block) @@ -13,6 +13,7 @@ module AASM::Core @to = opts[:to] @guards = Array(opts[:guards]) + Array(opts[:guard]) + Array(opts[:if]) @unless = Array(opts[:unless]) #TODO: This could use a better name + @failures = [] if opts[:on_transition] warn '[DEPRECATION] :on_transition is deprecated, use :after instead' @@ -53,9 +54,13 @@ module AASM::Core case code when Symbol, String arity = record.__send__(:method, code.to_sym).arity - arity == 0 ? record.__send__(code) : record.__send__(code, *args) + result = (arity == 0 ? record.__send__(code) : result = record.__send__(code, *args)) + failures << code unless result + result when Proc - code.parameters.size == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code) + result = (code.parameters.size == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code)) + failures << code.source_location.join('#') unless result + result when Array if options[:guard] # invoke guard callbacks diff --git a/lib/aasm/errors.rb b/lib/aasm/errors.rb index 72e2864..d9b5792 100644 --- a/lib/aasm/errors.rb +++ b/lib/aasm/errors.rb @@ -3,14 +3,18 @@ module AASM class UnknownStateMachineError < RuntimeError; end class InvalidTransition < RuntimeError - attr_reader :object, :event_name, :state_machine_name + attr_reader :object, :event_name, :state_machine_name, :failures - def initialize(object, event_name, state_machine_name) - @object, @event_name, @state_machine_name = object, event_name, state_machine_name + def initialize(object, event_name, state_machine_name, failures = []) + @object, @event_name, @state_machine_name, @failures = object, event_name, state_machine_name, failures end def message - "Event '#{event_name}' cannot transition from '#{object.aasm(state_machine_name).current_state}'" + "Event '#{event_name}' cannot transition from '#{object.aasm(state_machine_name).current_state}'. #{reasoning}" + end + + def reasoning + "Failed callback(s): #{@failures}." unless failures.empty? end end diff --git a/spec/unit/callback_multiple_spec.rb b/spec/unit/callback_multiple_spec.rb index c5c6ef2..d7fe11f 100644 --- a/spec/unit/callback_multiple_spec.rb +++ b/spec/unit/callback_multiple_spec.rb @@ -49,7 +49,8 @@ describe 'callbacks for the new DSL' do expect { callback.left_close! - }.to raise_error(AASM::InvalidTransition) + }.to raise_error(AASM::InvalidTransition, "Event 'left_close' cannot transition from 'open'. Failed callback(s): [:after_transition, :event_guard].") + end it "handles private callback methods as well" do @@ -87,7 +88,7 @@ describe 'callbacks for the new DSL' do expect { callback.left_close! - }.to raise_error(AASM::InvalidTransition) + }.to raise_error(AASM::InvalidTransition, "Event 'left_close' cannot transition from 'open'. Failed callback(s): [:after_transition, :event_guard, :transition_guard].") end it "does not run transition_guard twice for multiple permitted transitions" do @@ -132,10 +133,10 @@ describe 'callbacks for the new DSL' do expect(callback).to_not receive(:after_exit_open) expect(callback).to_not receive(:after_enter_closed) expect(callback).to_not receive(:after) - expect { callback.close! }.to raise_error(AASM::InvalidTransition) + end end @@ -277,12 +278,16 @@ describe 'event callbacks' do it 'should call it when transition failed for bang fire' do expect(@foo).to receive(:aasm_event_failed).with(:null, :open) - expect {@foo.null!}.to raise_error(AASM::InvalidTransition) + expect{ + @foo.null! + }.to raise_error(AASM::InvalidTransition, "Event 'null' cannot transition from 'open'. Failed callback(s): [:always_false].") end it 'should call it when transition failed for non-bang fire' do expect(@foo).to receive(:aasm_event_failed).with(:null, :open) - expect {@foo.null}.to raise_error(AASM::InvalidTransition) + expect{ + @foo.null + }.to raise_error(AASM::InvalidTransition, "Event 'null' cannot transition from 'open'. Failed callback(s): [:always_false, :always_false].") end it 'should not call it if persist fails for bang fire' do diff --git a/spec/unit/event_spec.rb b/spec/unit/event_spec.rb index cddeea9..fa0ea9e 100644 --- a/spec/unit/event_spec.rb +++ b/spec/unit/event_spec.rb @@ -109,6 +109,35 @@ describe 'firing an event' do expect(event.fire(obj, {}, nil, 'arg1', 'arg2')).to eq(:closed) end + context 'when given a gaurd proc' do + it 'should have access to callback failures in the transitions' do + event = AASM::Core::Event.new(:graduate, state_machine) do + transitions :to => :alumni, :from => [:student, :applicant], + :guard => Proc.new { 1 + 1 == 3 } + end + line_number = __LINE__ - 2 + obj = double('object', :aasm => double('aasm', :current_state => :student)) + + event.fire(obj, {}, nil) + expect(event.failed_callbacks).to eq ["#{__FILE__}##{line_number}"] + end + end + + context 'when given a guard symbol' do + it 'should have access to callback failures in the transitions' do + event = AASM::Core::Event.new(:graduate, state_machine) do + transitions :to => :alumni, :from => [:student, :applicant], + guard: :paid_tuition? + end + + obj = double('object', :aasm => double('aasm', :current_state => :student)) + allow(obj).to receive(:paid_tuition?).and_return(false) + + event.fire(obj, {}, nil) + expect(event.failed_callbacks).to eq [:paid_tuition?] + end + end + end describe 'should fire callbacks' do diff --git a/spec/unit/transition_spec.rb b/spec/unit/transition_spec.rb index 41a1634..05b313b 100644 --- a/spec/unit/transition_spec.rb +++ b/spec/unit/transition_spec.rb @@ -6,7 +6,7 @@ describe 'transitions' do process = ProcessWithNewDsl.new expect { process.stop! }.to raise_error do |err| expect(err.class).to eql(AASM::InvalidTransition) - expect(err.message).to eql("Event 'stop' cannot transition from 'sleeping'") + expect(err.message).to eql("Event 'stop' cannot transition from 'sleeping'. ") expect(err.object).to eql(process) expect(err.event_name).to eql(:stop) end @@ -145,6 +145,17 @@ describe AASM::Core::Transition, '- when performing guard checks' do expect(st.allowed?(obj)).to be false end + it 'should add the name of the failed method calls to the failures instance var' do + opts = {:from => 'foo', :to => 'bar', :guard => :test} + st = AASM::Core::Transition.new(event, opts) + + obj = double('object') + expect(obj).to receive(:test) + + st.allowed?(obj) + expect(st.failures).to eq [:test] + end + it 'should call the method on the object if unless is a symbol' do opts = {:from => 'foo', :to => 'bar', :unless => :test} st = AASM::Core::Transition.new(event, opts)