#14 new
Nathaniel Bibler

[Patch] Dynamically set initial state

Reported by Nathaniel Bibler | February 26th, 2009 @ 11:16 AM

Warning: Feature Request. :-D

I'd like the ability to dynamically assign the initial state of an object instead of strictly assigning it to one specific state.

While I realize that traditional state machines have an initial state, flow, and often final state (or are alternatively ever-running) and that this request somewhat breaks that basic concept, I think it is a very useful feature.

Proposed implementation

Maybe there's already a simple way to do this, but I don't immediately see it. Looking at the aasm_initial_state method, it currently only accepts nothing or a state name. I propose that we have a third option of a Proc, which gets executed at the time of creation. That Proc could then return the name of the desired initial state.

Highly contrived example

Take for instance the case where you're working with objects which are dependents of a parent. At their time of creation, their initial state may differ depending on the parent's state or conditions.

So, maybe you've signed up for a free version of an account and have attempted to create additional users beyond your quota. Instead of restricting that addition, it instead allows you to add the user, but sets them as disabled, for example. Normally, those created users would be allowed in the system, but once you're past quota, those users are disabled on creation.

I'm more than willing to submit the patch for it, but before putting in the effort I wanted to be sure it was something that seemed acceptable upstream. :)

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Library for adding state machines to Ruby classes. Includes persistence layers for things like ActiveRecord. Formerly known as acts_as_state_machine.

People watching this ticket