1

I have a rails model called Creative that implements a workflow using the aasm gem. Currently my model has just one workflow implemented in it.

I have a business scenario that will require me to implement another workflow in the same model which which will be activated if a boolean value on the model is true.

I see 2 approaches that could be viable options

  • Create a new model that uses the same table name as Creative and implement the workflow there
  • Implement the workflow in the same model using a separate column to store the states for the second workflow and use its method depending on my boolean value

What would be a good design that could be implemented here?

I understand this is a very open ended question and would love to get suggestions if anyone has come across such a scenario

Moiz Mansur
  • 180
  • 3
  • 13

1 Answers1

2

I think something like this should work.

event :promote do
  transitions :from => [:pending], :to => :in_progress, :guard => :boolean_check?
  transitions :from => [:pending], :to => :done
end

event :complete do
  transitions :from => [:in_progress], :to => :done, :guard => :boolean_check?
end

private

def boolean_check?
  self.boolean_column
end

If the boolean value is true the flow will be

pending > in_progress > done

else

pending > done

NOTE: This might get complex if let's say you have 3-4 workflows.

It's ok till you have 2 workflows

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
  • Thanks for this. This is something I could try out. Unfortunately the solution provided works in case of a small number of events. The workflow we require has quite a lot of different events and a guard would have to be added on each event and newer events would have to be added to the same workflow which would make it a non-readable piece of code. Thanks again though. Ill try this – Moiz Mansur Apr 22 '19 at 08:01