是否有可能从模型类中获取所有事件?我想按事件过滤一个列表。如何从模型类获取所有state_machine事件?
有关于所有转换的similar question on mailinglist。
我使用https://github.com/pluginaweek/state_machine
是否有可能从模型类中获取所有事件?我想按事件过滤一个列表。如何从模型类获取所有state_machine事件?
有关于所有转换的similar question on mailinglist。
我使用https://github.com/pluginaweek/state_machine
右键你链接到它GitHub的页面上显示的API方法:
vehicle.state_events # shows all the events possible from the current state
和
vehicle.state_paths.events # shows all the events for an object
是的,但是这是针对特定对象的,而不是模型类 – tonymarschall 2012-03-06 10:28:53
“Vehicle.new.state_events”或“Vehicle.new.state_paths.events”,那么......可能会有更直接的方式,如果你看看你链接的API,它似乎是每个对象获得“事件”集合。但我不使用那个状态机,所以我不能再多说了。 – Pavling 2012-03-06 16:25:56
这里有一些关于这个主题的更多信息:https://github.com/pluginaweek/state_machine/issues/165 – tonymarschall 2012-03-07 11:58:21
编辑2014:此列表的状态,不是事件
这是我做到了,只列出键
def self.membership_states
self.state_machines[:membership_status].states.map(&:name)
end
$ User.membership_states
> [:applied, ...
出于某种原因接受的答案需要几秒钟,我计算。如果你有很多状态,我想这会发生。
我什么工作原理是:
Invoice.state_machines[:state].events.map(&:name)
在我的情况下,类名是invoice
和state_machine被命名为state
。
好吧,我发现以前的问题回答这一个:http://stackoverflow.com/questions/4138437/get-list-of-state-machine-states – tonymarschall 2012-03-05 10:06:16
我必须纠正自己。以上问题的答案给出了所有过渡,而不是事件。 – tonymarschall 2012-03-05 10:18:59
这可能取决于您使用的是哪种状态机软件。 – Pavling 2012-03-05 21:09:34