you may now disable whiny transactions (using the new dsl)

This commit is contained in:
Thorsten Böttger 2011-11-26 20:49:46 +01:00
parent 5981d23f0b
commit 19275903a2
4 changed files with 49 additions and 7 deletions

View File

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

View File

@ -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={})

17
spec/models/silencer.rb Normal file
View File

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

View File

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