visualize state_machine‘s with graphviz

The guys at Plugin-a-week have written a nice ActiveRecord state machine gem called state_machine.

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:

Spree State Machine

Download the raw code (Requires ‘trollop’ gem).

  • Reddit
  • Technorati
  • Twitter
  • Facebook
  • Google Bookmarks
  • HackerNews
  • PDF
  • RSS
This entry was posted in rails and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.