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

Merge pull request #275 from chriswoodrich/whinier-transitions

Whinier transitions
This commit is contained in:
Thorsten Böttger 2016-01-11 22:48:10 +13:00
commit 1d53042fc6
7 changed files with 75 additions and 17 deletions

View file

@ -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

View file

@ -85,6 +85,10 @@ module AASM::Core
@transitions
end
def failed_callbacks
transitions.flat_map(&:failures)
end
private
def attach_event_guards(definitions)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)