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) self.aasm_event_failed(name, old_state.name)
end 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 end
rescue StandardError => e rescue StandardError => e
event.execute_error_callback(self, e) event.execute_error_callback(self, e)

View File

@ -4,6 +4,11 @@ module AASM
@clazz = clazz @clazz = clazz
sm = AASM::StateMachine[@clazz] sm = AASM::StateMachine[@clazz]
sm.config.column = options[:column].to_sym if options[:column] 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 end
def state(name, options={}) 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')) 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 describe AASM::SupportingClasses::StateTransition do
it 'should set from, to, and opts attr readers' do it 'should set from, to, and opts attr readers' do
opts = {:from => 'foo', :to => 'bar', :guard => 'g'} opts = {:from => 'foo', :to => 'bar', :guard => 'g'}