views:

6509

answers:

14

I'm looking to implement a state machine to manage a user moving through a series of steps over an extended period of time (weeks) with emails and then they interact with the app. I've looked at a couple of AASM plugins and forks (it seems like this plugin space has become a bit chaotic) and am curious what people would recommend.

I saw the automatic AASM by hashrocket, that transitions states using cron, and from the title it looks like it might fit the bill but there doesn't appear to be any documentation anywhere and it looks more like a skeleton app than a plugin.

+9  A: 

The RailsEnvy podcast talked about Alter Ego, a state machine implementation that sounds and looks pretty slick and it would be worth a look.

"AlterEgo is a Ruby implementation of the State pattern as described by the Gang of Four. It differs from other Ruby state machine libraries in that it focuses on providing polymorphic behavior based on object state. In effect, it makes it easy to give an object different “personalities” depending on the state it is in."

mwilliams
+6  A: 

I'm a big fan of state machines and have used the acts_as_state_machine plugin with good results. It's fairly well documented (in the "Practical Rails Plugins" book, for instance) and general purpose enough that you should be able to roll your own solution with it.

John
Do you use the plugin or the gem? Where are you getting it?
srboisvert
The AASM gem you have already is a rewrite that replaces acts_as_state_machine, I believe.
Otto
+1 for aasm, I've used it on many projects and it has worked extremely well
paulthenerd
+1  A: 

I have heard really good things about Ragel, but I haven't used it yet. So I don't know if it would be a good fir for your project, but it might be worth checking out

http://www.complang.org/ragel/

danmayer
+2  A: 

I am looking into Alter Ego. Found this tutorial from François Beausoleil as well.

srboisvert
+1  A: 

Maybe this is an overshoot, but there's a rails integrable bpm (workflow) engine called openwferu:

http://openwferu.rubyforge.org/

Here's the author on how to implement a state machine with it:

http://jmettraux.wordpress.com/2007/12/03/state-machine/

krusty.ar
+2  A: 

When we set up time-based state transitions, we basically used AASM and a cron job. You should be able to set up an event called something like "auto_advance" that has each of the possible transitions with the relevant conditions, and then your cron job and script/runner a script like this:

 MyModel.find(:all).each do |record|
   record.auto_advance!
 end
Matt Burke
+1  A: 

There's a great article in Rails Magazine Volume 3 on workflow solutions using acts_as_state_machine.

A: 

Here you have a state pattern implementation for ActiveRecord

dcadenas
A: 

I have found the State Machine Compiler to be very useful for creating state machines. It supports Ruby and many other languages.

Todd Stout
+5  A: 

I would recommend - http://github.com/pluginaweek/state%5Fmachine/ Designed to be used in any ruby app (as opposed to being activerecord/rails specific), but has an adapter for ActiveRecord.

Gokul
http://github.com/pluginaweek/state_machine
Kris
+1  A: 

A fully-featured ActiveRecord based state machine has apparently been knocking around in Edge for a few months now: http://blog.envylabs.com/2009/08/the-rails-state-machine/. Not clear what version is going to be the official release, however.

Tatyree
It has been removed from Rails 3.
holden
Looks like it was extracted out to this gem: http://github.com/qoobaa/transitions
Tyler Rick
A: 

Wonder what this means for external state machine implementations? About to start a new project and debating whether to start using AASM (which seems very close to Rails 3's version) or go ahead with something like state_machine which seems more powerful.

What do you guys think?

Karim Helal
+2  A: 

State Machine is the one that was supposed to be shipped with rails 3, I believe. It has been removed since. It can be used with any ruby class and offers integration with :

  • ActiveModel classes
  • ActiveRecord models
  • DataMapper resources
  • MongoMapper models
  • Sequel models

http://github.com/pluginaweek/state_machine

It also offers a very good documentation.

Bastien
Looks like it was extracted out to this gem: http://github.com/qoobaa/transitions
Tyler Rick
A: 

For most implementation you could use SimpleStateMachine, a simple DSL to decorate existing methods with state transition guards.

def invite
  send_activation_email
end
event :invite, :new => :invited