One of the great things about state machines is that they often can be visualized. To that end I’ve written a small utility that will load a class that uses state_machine and produce dot output which you can use with graphviz.
This gem is currently used by the spree shopping cart. Let’s look at an example of how this can be used to visualize the Order object in this project:
Save the gist below to visualize.rb and chmod +x visualize.rb. The arguments are 1) the path to RAILS_ROOT of the project using state_machine and 2) the class you want to visualize. Try visualize.rb --help to see all the options.
ruby visualize.rb . Order | dot -Tsvg -Grankdir=LR > graph.svg
Gives us the following image:
Download the raw code (Requires ‘trollop’ gem).