mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
1b422a537f
Example: class Foo include AASM aasm do state :initialized state :calculated state :finalized end end > Foo::STATE_INITIALIZED => :initialized > Foo::STATE_CALCULATED => :calculated You may find this useful in custom scopes when using ActiveRecord, or when testing your classes.
59 lines
1.8 KiB
Ruby
59 lines
1.8 KiB
Ruby
require 'spec_helper'
|
|
|
|
class Payment
|
|
include AASM
|
|
aasm do
|
|
state :initialised, :initial => true
|
|
state :filled_out
|
|
state :authorised
|
|
|
|
event :fill_out do
|
|
transitions :from => :initialised, :to => :filled_out
|
|
end
|
|
event :authorise do
|
|
transitions :from => :filled_out, :to => :authorised
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'state machine' do
|
|
let(:payment) {Payment.new}
|
|
|
|
it 'starts with an initial state' do
|
|
payment.aasm_current_state.should == :initialised
|
|
# payment.aasm.current_state.should == :initialised # not yet supported
|
|
payment.should respond_to(:initialised?)
|
|
payment.should be_initialised
|
|
end
|
|
|
|
it 'allows transitions to other states' do
|
|
payment.should respond_to(:fill_out)
|
|
payment.should respond_to(:fill_out!)
|
|
payment.fill_out!
|
|
payment.should respond_to(:filled_out?)
|
|
payment.should be_filled_out
|
|
|
|
payment.should respond_to(:authorise)
|
|
payment.should respond_to(:authorise!)
|
|
payment.authorise
|
|
payment.should respond_to(:authorised?)
|
|
payment.should be_authorised
|
|
end
|
|
|
|
it 'denies transitions to other states' do
|
|
lambda {payment.authorise}.should raise_error(AASM::InvalidTransition)
|
|
lambda {payment.authorise!}.should raise_error(AASM::InvalidTransition)
|
|
payment.fill_out
|
|
lambda {payment.fill_out}.should raise_error(AASM::InvalidTransition)
|
|
lambda {payment.fill_out!}.should raise_error(AASM::InvalidTransition)
|
|
payment.authorise
|
|
lambda {payment.fill_out}.should raise_error(AASM::InvalidTransition)
|
|
lambda {payment.fill_out!}.should raise_error(AASM::InvalidTransition)
|
|
end
|
|
|
|
it 'defines constants for each state name' do
|
|
Payment::STATE_INITIALISED.should eq(:initialised)
|
|
Payment::STATE_FILLED_OUT.should eq(:filled_out)
|
|
Payment::STATE_AUTHORISED.should eq(:authorised)
|
|
end
|
|
end
|