Validate next state before transitioning to it?

69 views
Skip to first unread message

wgrrrr

unread,
Mar 11, 2012, 3:39:32 PM3/11/12
to PluginAWeek: Talk
Hi all,

Loving this gem so much, so thanks to all involved for your hard work.

I tried to search for an answer to this, but came up short. Is it
possible to validate a state before transitioning to it? It's easy to
validate the current state, but it seems I can't figure out how to
validate than I can transition to the next state (i.e. ensure all
validations are true assuming the next state)?

For example:

state_machine :lifecycle_state, :initial => :pending do

state :pending do
end

state :active do
validates :common_description, :presence => true,
end

event :activate do
transition :pending => :active, :if => lambda {|object|
object.valid?}
end
end

In the above example, I'd like the :activate event to check if the
object is valid in the :active state, not the :pending state. Any
ideas?

Thanks!

Jon

unread,
Mar 11, 2012, 8:39:54 PM3/11/12
to pluginaw...@googlegroups.com
try :before_transition  ? or an if can work too?


----------------------------------------------------------------------------------
Jonathan Soong
DM4/NoQ DevOps.
Mob: 0403 347 994





--
You received this message because you are subscribed to the Google Groups "PluginAWeek: Talk" group.
To post to this group, send email to pluginaw...@googlegroups.com.
To unsubscribe from this group, send email to pluginaweek-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pluginaweek-talk?hl=en.


Aaron Pfeifer

unread,
Apr 29, 2012, 6:06:24 PM4/29/12
to pluginaw...@googlegroups.com
Hey there -

I apologize for taking so long to get back to you on this question.  I've heard several folks ask about this in the past and each time there was either a misunderstanding of how validations work in state_machine or the use case was solved by a slight change in the implementation.

Just to make sure we're on the same page, when you run an event, this will do the following (abbreviated version):
1. Run before transitions
2. Set the new state
3. Validate the object
4. Save the object
5. Run after transitions

Typically you don't need to check validation, do something else, and then proceed with saving the object.  Otherwise running the event would be sufficient since it'll return true / false if the validation does fail.

The only way to check validation without saving the object is by manually setting the state and running validations.

The other case where folks have been slightly confused about the behavior is that you can set :if / :unless conditionals on transition definitions 
so that you can prevent a transition from occurring based on some attribute(s) on the object.

Without knowing anything further about your particularly use case, these are the best tips I can really give.  Hope it provides some helps!

Best,
Aaron

Gurpartap Singh

unread,
Jul 27, 2012, 11:44:59 PM7/27/12
to pluginaw...@googlegroups.com
Here's how I did it *rolling eyes*

    event :start_allocation do
      transition :unallocated => :allocating
    end

    event :failed_allocation do
      transition :allocating => :unallocated
    end

    event :finish_allocation do
      transition :allocating => :allocated
    end
Reply all
Reply to author
Forward
0 new messages