1
0
Fork 0
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:
Thorsten Böttger 2015-07-20 20:55:47 +12:00
parent 2652abd4ec
commit 454f38e184
6 changed files with 75 additions and 1 deletions

View file

@ -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

View file

@ -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

View 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

View 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

View file

@ -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

View file

@ -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