0
votes

I am trying to figure out how to implement a dynamic approval system in Rails. The scenario is this: I have a model object (a document) which, when created, the user can assign an approval flow to. The document can be filled and submitted by a user. The document then goes through the approval flow before being "approved".

For example, let's say I create a "leave request" document. As a user I can then claim that the leave request must be approved by my line manager and someone from HR before being in the state "approved".

I have explored using a number of state-machine gems and CanCan for permissions, but I cannot wrap my head around how to dynamically create these workflows. I thought about serializing the workflow in the database, but this means that every time I want to determine a document approvers list of documents awaiting approval I will have to deserialize every workflow in the list of documents in the "awaiting approval" state.

Has anyone got any ideas as to how to tackle this problem?

1
Can you explain a bit more about what you mean by "dynamic"? Are you saying that each document may need approval by different types of users and this is decided when the document is created?benjaminjosephw
Hi, thanks for your response. So what I mean by dynamic is that each document may have not just different approvers, but a different path to "approved" through those approvers. So document of type A might have to be approved by "someone at HR" while document of type B might have to be approved by "someone at HR" and "the users line manager". Documents of type C may have an approval path of "HR Manager" and then "line manager". A more complex document of type D may have an approval path of "HR Manager" and "Line Manager" and then "CEO", etc.rshibli

1 Answers

0
votes

I think the best solution is to use separate table for storing approvers. You can dynamicly create 'approvers' when you create a document and then change each 'approver' independing of others. A document is approved if it does not have unapproved 'approvers'.

class Approver < ActiveRecord::Base
  attr_accessible :approved, role, ...
  # where role is manager or someone from HR or ...
  # approved is a status (true or false)

  belongs_to :document
  ...
end

class Document < ActiveRecord::Base
  has_many :approvers

  def approved?
    approvers.where(approved: false).empty?
  end

  ....
end