开发者

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

开发者 https://www.devze.com 2023-02-14 20:50 出处:网络
I\'m using a ActiveRecord::Transitions in Rails 3 and have my state machine defines as: state_machine do

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 @produc开发者_JAVA百科t in state "active" it should tell me

  • "disabled" is the only state available, or
  • "disable" is the only event available


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)


This answer is now more relevant

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

0

精彩评论

暂无评论...
验证码 换一张
取 消