[英]Transitions class (state machine) get a list of possible transitions
我在Rails 3中使用ActiveRecord::Transitions
,并将状态机定义为:
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
如何查看当前对象和状态的可用转换列表? 例如,如果我有一个@product
处于“活动”状态,则应该告诉我
我看不到任何明显的方法来枚举可能的下一个状态,但是您可以查询可用的事件,如下所示:
YourClass.state_machines[:default].events_for(:active)
=> [:disable]
(如果您有多个状态机,则YourClass.state_machines
哈希中将有其他成员)
基本上-您可以访问@product.state_evants
, @product.state_transitions
和@product.state_paths
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.