mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
add basic examples with two state machines
This commit is contained in:
parent
2652abd4ec
commit
454f38e184
6 changed files with 75 additions and 1 deletions
|
@ -55,4 +55,9 @@ ActiveRecord::Migration.suppress_messages do
|
|||
t.string "aasm_state"
|
||||
t.string "type"
|
||||
end
|
||||
|
||||
ActiveRecord::Migration.create_table "basic_active_record_two_state_machines_examples", :force => true do |t|
|
||||
t.string "search"
|
||||
t.string "sync"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
class BasicActiveRecordTwoStateMachinesExample < ActiveRecord::Base
|
||||
include AASM
|
||||
|
||||
aasm :search, :column => :search do
|
||||
state :initialised, :initial => true
|
||||
state :queried
|
||||
state :requested
|
||||
|
||||
event :query do
|
||||
transitions :from => [:initialised, :requested], :to => :queried
|
||||
end
|
||||
event :request do
|
||||
transitions :from => :queried, :to => :requested
|
||||
end
|
||||
end
|
||||
|
||||
aasm :sync, :column => :sync do
|
||||
state :unsynced, :initial => true
|
||||
state :synced
|
||||
|
||||
event :synchronise do
|
||||
transitions :from => :unsynced, :to => :synced
|
||||
end
|
||||
end
|
||||
end
|
25
spec/models/basic_two_state_machines_example.rb
Normal file
25
spec/models/basic_two_state_machines_example.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
class BasicTwoStateMachinesExample
|
||||
include AASM
|
||||
|
||||
aasm :search do
|
||||
state :initialised, :initial => true
|
||||
state :queried
|
||||
state :requested
|
||||
|
||||
event :query do
|
||||
transitions :from => [:initialised, :requested], :to => :queried
|
||||
end
|
||||
event :request do
|
||||
transitions :from => :queried, :to => :requested
|
||||
end
|
||||
end
|
||||
|
||||
aasm :sync do
|
||||
state :unsynced, :initial => true
|
||||
state :synced
|
||||
|
||||
event :sync do
|
||||
transitions :from => :unsynced, :to => :synced
|
||||
end
|
||||
end
|
||||
end
|
10
spec/unit/basic_two_state_machines_example_spec.rb
Normal file
10
spec/unit/basic_two_state_machines_example_spec.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'on initialization' do
|
||||
let(:example) { BasicTwoStateMachinesExample.new }
|
||||
|
||||
it 'should be in the initial state' do
|
||||
expect(example.aasm(:search).current_state).to eql :initialised
|
||||
expect(example.aasm(:sync).current_state).to eql :unsynced
|
||||
end
|
||||
end
|
|
@ -3,7 +3,7 @@ require 'spec_helper'
|
|||
describe 'on initialization' do
|
||||
let(:auth) {ComplexExampleMultiple.new}
|
||||
|
||||
it 'should be in the pending state' do
|
||||
it 'should be in the initial state' do
|
||||
expect(auth.aasm(:left).current_state).to eq(:pending)
|
||||
expect(auth.aasm(:right).current_state).to eq(:pending)
|
||||
end
|
||||
|
|
|
@ -512,3 +512,12 @@ describe "invalid states with persistence" do
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'basic example with two state machines' do
|
||||
let(:example) { BasicActiveRecordTwoStateMachinesExample.new }
|
||||
|
||||
it 'should initialise properly' do
|
||||
expect(example.aasm(:search).current_state).to eql :initialised
|
||||
expect(example.aasm(:sync).current_state).to eql :unsynced
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue