diff --git a/PLANNED_CHANGES.md b/PLANNED_CHANGES.md index b60c326..cfa3670 100644 --- a/PLANNED_CHANGES.md +++ b/PLANNED_CHANGES.md @@ -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 diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index e9ffdc1..cf9879c 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -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 diff --git a/lib/aasm/errors.rb b/lib/aasm/errors.rb index 9c8637e..72e2864 100644 --- a/lib/aasm/errors.rb +++ b/lib/aasm/errors.rb @@ -1,5 +1,7 @@ module AASM + class UnknownStateMachineError < RuntimeError; end + class InvalidTransition < RuntimeError attr_reader :object, :event_name, :state_machine_name diff --git a/spec/unit/edge_cases_spec.rb b/spec/unit/edge_cases_spec.rb new file mode 100644 index 0000000..7100820 --- /dev/null +++ b/spec/unit/edge_cases_spec.rb @@ -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