ROR: Gem Workflow: I want to implement dynamic states from a database

I am working on a project that needs to implement a dynamic workflow.

Dynamic . I store the states of the workflow in a database table named wf_steps and the workflow gem has to create states

for a specific workflow from the database

I am trying to use the workflow color for this . You can see how it initializes states and related events in the gem.

My code:

class SpsWorkflow < ActiveRecord::Base
  belongs_to :user
  has_many :wf_steps
  include Workflow
  workflow do
    # binding.pry
    # read wf-states from the database
    # for now let event be same for all the states
    self.wf_steps.each do |step|
      state step.title.to_sym do
        event :assign, transitions_to: :assigning
        event :hire, transitions_to: :hiring
        event :not_hire, transitions_to: :not_hiring
      end
    end
  end
end

      

Waiting and problem encountered:

I expected the term to self.wf_steps

return an instance / collection in the code block below SpsWorkflow

. However, the keyword self

returns #<Workflow::Specification:0x000000063e23e8 @meta={}, @states={}>

when I use binding.pry

inside a method block workflow

(I commented out the code)

# read wf-states from the database
# for now let event be same for all the states
  self.wf_steps.each do |step|
    state step.title.to_sym do

      

You need help, thanks

EDIT: I also tried to store the instance in a variable and use the variable inside the block going through to the method call workflow

.

class SpsWorkflow < ActiveRecord::Base
  include Workflow
  sps_instance = self

      

But I got an instance of a class SpsWorkflow

like

SpsWorkflow(id: integer, workflow_state: string, assigned_to: integer, title: string, description: string, organization_id: integer, user_id: integer, created_at: datetime, updated_at: datetime)

      

but I want

 #<SpsWorkflow id: 1, workflow_state: "step1", assigned_to: nil, title: "Software Engineer", description: "Hire best engineer", organization_id: nil, user_id: 1, created_at: "2015-08-08 00:58:12", updated_at: "2015-08-08 00:58:12">

      

0


source to share


2 answers


I finally solved it using the activerecord callback



class SpsWorkflow < ActiveRecord::Base
  include Workflow
  after_initialize do
    sps_instance = self
    SpsWorkflow.workflow do
      # read wf-states as well as events from the database
      sps_instance.wf_steps.each do |step|
        state step.title.to_sym do
          event :assign, transitions_to: :step2
          event :hire, transitions_to: :hire
          event :not_hire, transitions_to: :not_hiring
        end
      end
    end
  end
  belongs_to :user
  has_many :wf_steps
end

      

0


source


Have you used:

workflow do
  self.something
end

      



self

in the context of this block will refer to WorkflowSpecification

. If you really need access to the instance SpsWorkflow

, you might have to pass it to a block, or assign it to another variable and use it there.

0


source







All Articles