AASM Diagram creates diagrams for AASM state machines. For example, lets take Job model from the AASM Readme:
class Job
include AASM
aasm do
state :sleeping, :initial => true
state :running, :cleaning
event :run do
transitions :from => :sleeping, :to => :running
end
event :clean do
transitions :from => :running, :to => :cleaning
end
event :sleep do
transitions :from => [:running, :cleaning], :to => :sleeping
end
end
end
Now in your console you can execute:
>> require 'aasm-diagram'
>> job = Job.new
>> AASMDiagram::Diagram.new(job.aasm, 'tmp/job.png')
These commands will generate the following diagram:
- Install Graphviz (see the rails-erd guide for more details). If you use Homebrew try
brew install graphviz
. - Add
gem 'aasm-diagram', github: 'Mayurifag/aasm-diagram', require: false, group: :development
to yourGemfile
and runbundle
.
class Cleaner
include AASM
aasm do
state :idle, :initial => true
state :cleaning
event :clean do
transitions :from => :idle, :to => :cleaning, :guard => :cleaning_needed?
end
event :clean_if_needed do
transitions :from => :idle, :to => :cleaning do
guard do
cleaning_needed?
end
end
transitions :from => :idle, :to => :idle
end
event :clean_if_dirty do
transitions :from => :idle, :to => :cleaning, :guard => :if_dirty?
end
end
def cleaning_needed?
false
end
def if_dirty?(status)
status == :dirty
end
end
cleaner = Cleaner.new
AASMDiagram::Diagram.new(job.aasm, '../docs/guard-cleaner.png')
class SimpleMultipleExample
include AASM
aasm(:move) do
state :standing, :initial => true
state :walking
state :running
event :walk do
transitions :from => :standing, :to => :walking
end
event :run do
transitions :from => [:standing, :walking], :to => :running
end
event :hold do
transitions :from => [:walking, :running], :to => :standing
end
end
aasm(:work) do
state :sleeping, :initial => true
state :processing
event :start do
transitions :from => :sleeping, :to => :processing
end
event :stop do
transitions :from => :processing, :to => :sleeping
end
end
end
simple = SimpleMultipleExample.new
AASMDiagram::Diagram.new(simple.aasm(:move), 'docs/multiple-state-machines-1.png')
AASMDiagram::Diagram.new(simple.aasm(:work), 'docs/multiple-state-machines-2.png')
Generates two images:
AASM Diagram is heavily inspired by rails-erd.
The gem is available as open source under the terms of the MIT License.