reference state machine object and event in invalid transition error so end user can intelligently handle
This commit is contained in:
parent
123700ce4e
commit
5a8454221f
|
@ -129,7 +129,7 @@ private
|
||||||
end
|
end
|
||||||
|
|
||||||
if AASM::StateMachine[self.class].config.whiny_transitions
|
if AASM::StateMachine[self.class].config.whiny_transitions
|
||||||
raise AASM::InvalidTransition, "Event '#{event_name}' cannot transition from '#{aasm.current_state}'"
|
raise AASM::InvalidTransition.new(self, event_name)
|
||||||
else
|
else
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,16 @@
|
||||||
module AASM
|
module AASM
|
||||||
class InvalidTransition < RuntimeError; end
|
|
||||||
|
class InvalidTransition < RuntimeError
|
||||||
|
attr_reader :object, :event_name
|
||||||
|
def initialize(object, event_name)
|
||||||
|
@object, @event_name = object, event_name
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
"Event '#{event_name}' cannot transition from '#{object.aasm.current_state}'"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class UndefinedState < RuntimeError; end
|
class UndefinedState < RuntimeError; end
|
||||||
class NoDirectAssignmentError < RuntimeError; end
|
class NoDirectAssignmentError < RuntimeError; end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,12 @@ describe 'transitions' do
|
||||||
|
|
||||||
it 'should raise an exception when whiny' do
|
it 'should raise an exception when whiny' do
|
||||||
process = ProcessWithNewDsl.new
|
process = ProcessWithNewDsl.new
|
||||||
expect { process.stop! }.to raise_error(AASM::InvalidTransition)
|
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.object).to eql(process)
|
||||||
|
expect(err.event_name).to eql(:stop)
|
||||||
|
end
|
||||||
expect(process).to be_sleeping
|
expect(process).to be_sleeping
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue