handle edge cases when a state machine is accessed which doesn't exist

This commit is contained in:
Thorsten Böttger 2015-08-05 22:00:22 +12:00
parent 7ea9b93d6c
commit d540ca8761
4 changed files with 23 additions and 2 deletions

View File

@ -11,8 +11,6 @@
# Currently working on
* add support for multiple state machines per class
* what happen's if someone accesses `aasm`, but has defined a
state machine for `aasm(:my_name)`?
# Changes so far
@ -29,4 +27,6 @@
* _Mongoid_
* _MongoMapper_
* _Sequel_
* what happen's if someone accesses `aasm`, but has defined a
state machine for `aasm(:my_name)`?
* drop support for find_in_state, count_in_state, calculate_in_state, with_state_scope

View File

@ -47,6 +47,9 @@ module AASM
# this is the entry point for all instance-level access to AASM
def aasm(name=:default)
unless AASM::StateMachine[self.class][name.to_sym]
raise AASM::UnknownStateMachineError.new("There is no state machine with the name '#{name}' defined in #{self.class.name}!")
end
@aasm ||= {}
@aasm[name.to_sym] ||= AASM::InstanceBase.new(self, name.to_sym)
end

View File

@ -1,5 +1,7 @@
module AASM
class UnknownStateMachineError < RuntimeError; end
class InvalidTransition < RuntimeError
attr_reader :object, :event_name, :state_machine_name

View File

@ -0,0 +1,16 @@
require 'spec_helper'
describe "edge cases" do
describe "for classes with multiple state machines" do
it "allows accessing a multiple state machine class without state machine name" do
# it's like starting to define a new state machine within the
# requested class
expect(SimpleMultipleExample.aasm.states.map(&:name)).to be_empty
end
it "do not know yet" do
example = ComplexExampleMultiple.new
expect { example.aasm.states.inspect }.to raise_error(AASM::UnknownStateMachineError)
end
end
end