4

Is it possible to get all events from a model class? I want to filter a list by events.

There was a similar question on mailinglist about all transitions.

I am using https://github.com/pluginaweek/state_machine

tonymarschall
  • 3,862
  • 3
  • 29
  • 52

3 Answers3

6

Right on the github page you link to it shows the API methods for:

vehicle.state_events  # shows all the events possible from the current state

and

vehicle.state_paths.events # shows all the events for an object
Pavling
  • 3,933
  • 1
  • 22
  • 25
  • Yes, but this is for a specific object, not the model class – tonymarschall Mar 06 '12 at 10:28
  • "Vehicle.new.state_events" or "Vehicle.new.state_paths.events" then... there's probably a more direct way, if you look through the API you linked, it does seem to be that each object gets an "events" collection. But I don't use that state machine, so I can't say more than that. – Pavling Mar 06 '12 at 16:25
  • Here are some more informations about this topic: https://github.com/pluginaweek/state_machine/issues/165 – tonymarschall Mar 07 '12 at 11:58
2

The accepted answer for some reason takes a few seconds for me to compute. I guess this happens if you have a lot of states.

What works for me is:

Invoice.state_machines[:state].events.map(&:name)

In my case the class name is invoice and the state_machine is named state.

Hendrik
  • 4,849
  • 7
  • 46
  • 51
0

edit 2014: this list states, not events

This is how I did it, to only list the keys

def self.membership_states
  self.state_machines[:membership_status].states.map(&:name)
end

$ User.membership_states
> [:applied, ...
oma
  • 38,642
  • 11
  • 71
  • 99