Skip to content

Commit

Permalink
you may now disable whiny transactions (using the new dsl)
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Nov 26, 2011
1 parent 5981d23 commit 1927590
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/aasm/aasm.rb
Expand Up @@ -197,7 +197,11 @@ def aasm_fire_event(name, options, *args)
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)
Expand Down
5 changes: 5 additions & 0 deletions lib/aasm/base.rb
Expand Up @@ -4,6 +4,11 @@ def initialize(clazz, options={}, &block)
@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={})
Expand Down
17 changes: 17 additions & 0 deletions 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
28 changes: 22 additions & 6 deletions 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'}
Expand Down Expand Up @@ -116,9 +132,9 @@

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)
Expand All @@ -127,9 +143,9 @@

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)
Expand All @@ -144,7 +160,7 @@

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)
Expand All @@ -159,5 +175,5 @@

return_value.should == 'success'
end

end

0 comments on commit 1927590

Please sign in to comment.