Pergunta

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?

Foi útil?

Solução

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.:

Transition.create(:from => "parked", :to => "idling", :on => "ignite")

Then in your transitions method:

def transitions
  transitions_data = []
  Transition.all.each do |transition|
    transitions_data << { transition.from.to_sym => transition.to.to_sym, :on => transition.on.to_sym }  
  end
  transitions_data
end

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top