Zepplock
Zepplock

Reputation: 29155

Transitions class (state machine) get a list of possible transitions

I'm using a ActiveRecord::Transitions in Rails 3 and have my state machine defines as:

state_machine do
 state :initial # first one is initial state
 state :active
 state :disabled

 event :activate do
   transitions :to => :active, :from => [:initial, :disabled]
 end
 event :disable do
   transitions :to => :disabled, :from => [:initial, :active]
 end
end

How do I see a list of available transitions for a current object and state? For example if I have a @product in state "active" it should tell me

Upvotes: 3

Views: 2382

Answers (2)

tamersalama
tamersalama

Reputation: 4143

This answer is now more relevant

Basically - you have access to @product.state_evants, @product.state_transitions and @product.state_paths

Upvotes: 1

nickgrim
nickgrim

Reputation: 5437

I can't see any obvious way to enumerate possible-next-states, but you can query the available events like this:

YourClass.state_machines[:default].events_for(:active)
 => [:disable] 

(If you have more than one state machine there will be additional members in the YourClass.state_machines Hash)

Upvotes: 3

Related Questions