From 19275903a2aeaec34bab1c545b52344d254b18ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20Bo=CC=88ttger?= Date: Sat, 26 Nov 2011 20:49:46 +0100 Subject: [PATCH] you may now disable whiny transactions (using the new dsl) --- lib/aasm/aasm.rb | 6 +++++- lib/aasm/base.rb | 5 +++++ spec/models/silencer.rb | 17 +++++++++++++++++ spec/unit/state_transition_spec.rb | 28 ++++++++++++++++++++++------ 4 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 spec/models/silencer.rb diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 0a314d8..189fc5d 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -197,7 +197,11 @@ private self.aasm_event_failed(name, old_state.name) end - raise AASM::InvalidTransition, "Event '#{event.name}' cannot transition from '#{self.aasm_current_state}'" + if AASM::StateMachine[self.class].config.whiny_transitions + raise AASM::InvalidTransition, "Event '#{event.name}' cannot transition from '#{self.aasm_current_state}'" + else + false + end end rescue StandardError => e event.execute_error_callback(self, e) diff --git a/lib/aasm/base.rb b/lib/aasm/base.rb index fef0953..3decc13 100644 --- a/lib/aasm/base.rb +++ b/lib/aasm/base.rb @@ -4,6 +4,11 @@ module AASM @clazz = clazz sm = AASM::StateMachine[@clazz] sm.config.column = options[:column].to_sym if options[:column] + if options.key?(:whiny_transitions) + sm.config.whiny_transitions = options[:whiny_transitions] + else + sm.config.whiny_transitions = true # this is the default, so let's cry + end end def state(name, options={}) diff --git a/spec/models/silencer.rb b/spec/models/silencer.rb new file mode 100644 index 0000000..b679f80 --- /dev/null +++ b/spec/models/silencer.rb @@ -0,0 +1,17 @@ +class Silencer + include AASM + + aasm :whiny_transitions => false do + state :silent, :initial => true + state :crying + state :smiling + + event :cry do + transitions :from => :silent, :to => :crying + end + event :smile do + transitions :from => :crying, :to => :smiling + end + end + +end diff --git a/spec/unit/state_transition_spec.rb b/spec/unit/state_transition_spec.rb index 2027b93..bef98e1 100644 --- a/spec/unit/state_transition_spec.rb +++ b/spec/unit/state_transition_spec.rb @@ -1,5 +1,21 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper')) +describe 'transitions' do + + it 'should raise an exception when whiny' do + process = ProcessWithNewDsl.new + lambda { process.stop! }.should raise_error(AASM::InvalidTransition) + process.should be_sleeping + end + + it 'should not raise an exception when whiny' do + silencer = Silencer.new + silencer.smile!.should be_false + silencer.should be_silent + end + +end + describe AASM::SupportingClasses::StateTransition do it 'should set from, to, and opts attr readers' do opts = {:from => 'foo', :to => 'bar', :guard => 'g'} @@ -116,9 +132,9 @@ describe AASM::SupportingClasses::StateTransition, '- when executing the transit obj.should_receive(:test) - st.execute(obj, args) + st.execute(obj, args) end - + it 'should accept a Symbol for the method name' do opts = {:from => 'foo', :to => 'bar', :on_transition => :test} st = AASM::SupportingClasses::StateTransition.new(opts) @@ -127,9 +143,9 @@ describe AASM::SupportingClasses::StateTransition, '- when executing the transit obj.should_receive(:test) - st.execute(obj, args) + st.execute(obj, args) end - + it 'should pass args if the target method accepts them' do opts = {:from => 'foo', :to => 'bar', :on_transition => :test} st = AASM::SupportingClasses::StateTransition.new(opts) @@ -144,7 +160,7 @@ describe AASM::SupportingClasses::StateTransition, '- when executing the transit return_value.should == 'success' end - + it 'should NOT pass args if the target method does NOT accept them' do opts = {:from => 'foo', :to => 'bar', :on_transition => :test} st = AASM::SupportingClasses::StateTransition.new(opts) @@ -159,5 +175,5 @@ describe AASM::SupportingClasses::StateTransition, '- when executing the transit return_value.should == 'success' end - + end