2010-08-27 16:35:26 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2008-01-07 14:11:38 -05:00
|
|
|
|
|
|
|
describe AASM::SupportingClasses::Event do
|
|
|
|
before(:each) do
|
|
|
|
@name = :close_order
|
2008-05-20 15:27:35 -04:00
|
|
|
@success = :success_callback
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_event
|
2008-05-20 15:27:35 -04:00
|
|
|
@event = AASM::SupportingClasses::Event.new(@name, {:success => @success}) do
|
2008-01-07 14:11:38 -05:00
|
|
|
transitions :to => :closed, :from => [:open, :received]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the name' do
|
|
|
|
new_event
|
|
|
|
@event.name.should == @name
|
|
|
|
end
|
|
|
|
|
2008-05-20 15:27:35 -04:00
|
|
|
it 'should set the success option' do
|
|
|
|
new_event
|
|
|
|
@event.success.should == @success
|
|
|
|
end
|
|
|
|
|
2008-02-21 10:16:08 -05:00
|
|
|
it 'should create StateTransitions' do
|
2008-01-07 14:11:38 -05:00
|
|
|
AASM::SupportingClasses::StateTransition.should_receive(:new).with({:to => :closed, :from => :open})
|
|
|
|
AASM::SupportingClasses::StateTransition.should_receive(:new).with({:to => :closed, :from => :received})
|
|
|
|
new_event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-02-21 10:16:08 -05:00
|
|
|
describe AASM::SupportingClasses::Event, 'when firing an event' do
|
|
|
|
it 'should raise an AASM::InvalidTransition error if the transitions are empty' do
|
2008-08-13 17:56:16 -04:00
|
|
|
obj = mock('object')
|
|
|
|
obj.stub!(:aasm_current_state)
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2008-08-13 17:56:16 -04:00
|
|
|
event = AASM::SupportingClasses::Event.new(:event)
|
|
|
|
lambda { event.fire(obj) }.should raise_error(AASM::InvalidTransition)
|
2008-02-21 10:16:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the state of the first matching transition it finds' do
|
|
|
|
event = AASM::SupportingClasses::Event.new(:event) do
|
|
|
|
transitions :to => :closed, :from => [:open, :received]
|
|
|
|
end
|
|
|
|
|
|
|
|
obj = mock('object')
|
|
|
|
obj.stub!(:aasm_current_state).and_return(:open)
|
|
|
|
|
|
|
|
event.fire(obj).should == :closed
|
|
|
|
end
|
|
|
|
end
|
2008-10-09 10:53:39 -04:00
|
|
|
|
|
|
|
describe AASM::SupportingClasses::Event, 'when executing the success callback' do
|
|
|
|
class ThisNameBetterNotBeInUse
|
|
|
|
include AASM
|
|
|
|
|
|
|
|
aasm_state :initial
|
|
|
|
aasm_state :symbol
|
|
|
|
aasm_state :string
|
|
|
|
aasm_state :array
|
|
|
|
aasm_state :proc
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should send the success callback if it's a symbol" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
|
|
|
aasm_event :with_symbol, :success => :symbol_success_callback do
|
|
|
|
transitions :to => :symbol, :from => [:initial]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
model = ThisNameBetterNotBeInUse.new
|
|
|
|
model.should_receive(:symbol_success_callback)
|
|
|
|
model.with_symbol!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should send the success callback if it's a string" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
|
|
|
aasm_event :with_string, :success => 'string_success_callback' do
|
|
|
|
transitions :to => :string, :from => [:initial]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
model = ThisNameBetterNotBeInUse.new
|
|
|
|
model.should_receive(:string_success_callback)
|
|
|
|
model.with_string!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call each success callback if passed an array of strings and/or symbols" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
|
|
|
aasm_event :with_array, :success => [:success_callback1, 'success_callback2'] do
|
|
|
|
transitions :to => :array, :from => [:initial]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
model = ThisNameBetterNotBeInUse.new
|
|
|
|
model.should_receive(:success_callback1)
|
|
|
|
model.should_receive(:success_callback2)
|
|
|
|
model.with_array!
|
|
|
|
end
|
|
|
|
|
2009-04-09 01:14:02 -04:00
|
|
|
it "should call each success callback if passed an array of strings and/or symbols and/or procs" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
|
|
|
aasm_event :with_array_including_procs, :success => [:success_callback1, 'success_callback2', lambda { |obj| obj.proc_success_callback }] do
|
|
|
|
transitions :to => :array, :from => [:initial]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
model = ThisNameBetterNotBeInUse.new
|
|
|
|
model.should_receive(:success_callback1)
|
|
|
|
model.should_receive(:success_callback2)
|
|
|
|
model.should_receive(:proc_success_callback)
|
|
|
|
model.with_array_including_procs!
|
|
|
|
end
|
|
|
|
|
2008-10-09 10:53:39 -04:00
|
|
|
it "should call the success callback if it's a proc" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
|
|
|
aasm_event :with_proc, :success => lambda { |obj| obj.proc_success_callback } do
|
|
|
|
transitions :to => :proc, :from => [:initial]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
model = ThisNameBetterNotBeInUse.new
|
|
|
|
model.should_receive(:proc_success_callback)
|
|
|
|
model.with_proc!
|
|
|
|
end
|
|
|
|
end
|