Skip to content

Adapter to allow ActiveRecord to persist and restore state of objects using the AlterEgo state machine

Notifications You must be signed in to change notification settings

pavlos/alter-ego-activerecord

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AlterEgo-ActiveRecord

Motivation

AlterEgo is my favorite Ruby state machine for the following reasons:

1. It's not dependent on ActiveRecord - it can be used on plain Ruby objects.
2. It most closely follows the GOF State Pattern because it allows for 
   polymorphic behavior based on state.

Out of the box, AlterEgo doesn’t play nicely with ActiveRecord because it stores state in @state, whereas subclasses of ActiveRecord::Base persist their attributes hash.

This mixin overrides the AlterEgo’s accessor methods for state to allow it to be properly persisted to a database, as well as serialized/unserialized as json, yml, and xml.

Installation

As a Ruby Gem

gem install alter-ego-activerecord

OR as a Rails plugin

script/plugin install git://github.com/pavlos/alter-ego-activerecord.git

Usage

Make sure the table your class maps to has a state column of type varchar, string, etc.

# you'll only need the following two lines if you're NOT using
# alter-ego-activerecord as a plugin
gem 'alter-ego-activerecord'
require 'alter_ego/active_record_adapter'

class Example < ActiveRecord::Base
  include AlterEgo # include this first
  include AlterEgo::ActiveRecordAdapter

  # Your code here
end

About

Adapter to allow ActiveRecord to persist and restore state of objects using the AlterEgo state machine

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages