I am trying to write an application that will allow users to manage workflows using the State Machine gem but I am not sure how to proceed in allowing users to define their own state machines using the State Machine gem for ruby.
In the dynamic definitions portion of the gem documentation it says that I should be able to do this by replacing code like this below with a data source.
def transitions
[
{:parked => :idling, :on => :ignite},
{:idling => :first_gear, :first_gear => :second_gear, :on => :shift_up}
# ...
]
end
I am not sure how to go about doing this. how do I define the transistions from a database?
Because
transitions
is just a method, you could implement this any way you want. Here's one possible way.I'll assume you're using ActiveRecord.
Define a Transition model and an associated transitions table with to, from, and on columns, all strings. Then you can start defining transitions, e.g.:
Then in your transitions method:
You can then use the other code in the documentation you linked to create a state machine dynamically.
This is just one example, and could be much further optimized. I'll leave that part to you. Hopefully this will give you a good start.