【问题标题】:ROR : Workflow gem : I want to implement dynamic states from databaseROR:工作流 gem:我想从数据库实现动态状态
【发布时间】:2015-08-10 03:01:57
【问题描述】:

我目前正在从事一个必须实施动态工作流程的项目。

动态:我将工作流的状态存储在名为 wf_steps 的数据库表中,并且工作流 gem 必须为数据库中的特定工作流创建 states

为此,我尝试使用workflow gem。您可以在 gem 的 github-page 中看到它是如何初始化状态和相应事件的。

我的代码:

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

期望和遇到的问题:

我预计在术语 self.wf_steps 下方的代码块中将返回我的 SpsWorkflow 的实例/集合。但是,当我在 workflow 方法的块内使用 binding.pry 时,self 关键字返回 #&lt;Workflow::Specification:0x000000063e23e8 @meta={}, @states={}&gt;(我在代码中注释了)

# 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

需要你帮忙,谢谢

编辑: 我还尝试将实例存储在一个变量中,并使用传递给workflow 方法调用的块内的变量。

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

但是我得到了 SpsWorkflow 类的实例

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)

但我想要

 #<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">

【问题讨论】:

    标签: ruby-on-rails ruby workflow metaprogramming


    【解决方案1】:

    您使用过:

    workflow do
      self.something
    end
    

    self 在此块的上下文中将引用WorkflowSpecification。如果你真的想访问SpsWorkflow 的实例,你可能必须将它传递到块中或将其分配给不同的变量并在那里使用它。

    【讨论】:

    • 谢谢,我尝试使用变量current_instance = self 传递当前实例,但它存储了类的实例,例如`SpsWorkflow(id: integer, workflow_state: string, assigned_to: integer, title: string,描述:字符串,组织 ID:整数,用户 ID:整数,created_at:日期时间,更新时间:日期时间)` 但我需要 #&lt;SpsWorkflow id: 1, workfl.... 的实例
    【解决方案2】:

    我终于用 activerecord 回调解决了这个问题

    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
    

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多