SideState

SideState is an extention for AASM, which stores the state in a separate object (and thus providing historical information on the state changes for a given record).

Installation

gem install sidestate

Don’t forget to add it to your environment.rb or Gemfile.

Usage

You’ll want to set up AASM as normal, with states, events and transitions. To get SideState working nicely, you’ll want to include the module of the same name into your model:

class Article < ActiveRecord::Base
  include AASM
  include Sidestate
  
  # ...
end

Then, you’ll want the model and migration generated for the companion state object:

script/generate sidestate article

You can add whatever you like to the new model (in this example, it would be called ArticleState), but make sure you don’t delete the foreign key and name columns. It’s also a good idea to add an index to the foreign key column (the generator isn’t smart enough to do this yet).

Important Note: Sidestate adds a has_many association from the main class to the state class, and a belongs_to one in the other direction. You don’t need to (and shouldn’t) add these yourself.

Another Important Note: You will still need the state column (default is aasm_state) in your main class – this makes querying on the current state of objects much easier, and SideState expects it as well.

And that’s pretty much it – whenever the state changes on your main class, a new state object will be saved to the database as well. Just keep using AASM as you normally do.

Warning

Very much a beta, and might change dramatically. Seems to work in basic situations, at least. Your mileage may vary.

Credits

Copyright © 2010 Envato. Initially developed by Pat Allan. Released under an MIT licence.