Jon
Jon

Reputation: 833

Need clarification of Ruby state machine usage

My problem is that I have an application that gives a user the ability to define a workflow (states, transitions, events, etc.) and have my application know how it reacts (transitions) based on the users workflow.

I have looked at several state machine gems, such as AASM, and I see how I could use the gem to pre-define the state machine but if the state machine needed to change, it looks like I would have to modify the code and re-deploy. I have been arguing with my coworker about how we could / could not utilize a state machine gem to do what we want but, to me, they all seem to define a static state machine and changes to that state machine require code changes.

His suggestion is dynamically modifying the Ruby class to match the users workflow changes. My thought is that the states, transitions, events, guards, etc. are persistable objects that the user modifies through the our API. Neither of our current lines of thought seem to work with the current Ruby state machines without some major modification on top of those gems.

The place I keep looking at as an example solution is JIRA and how you can define states, transitions, and other workflow attributes for a project dynamically.

Upvotes: 3

Views: 496

Answers (2)

Aristata
Aristata

Reputation: 640

The state_machine gem allows this:

https://github.com/pluginaweek/state_machine#static--dynamic-definitions

Upvotes: 0

Alex Ponomarev
Alex Ponomarev

Reputation: 945

State machine should be used when business logic is pre-defined, otherwise you should model it in different way. If you want users to create their own workflow (e.g. states, transitions, events), then it would make sense to create separate models for state, transition and event, and define their relationships in code.

Then you can have, say, several kinds of transitions and events, that have different outcomes. If you could describe in detail what kind of dynamic workflow you need I could tell you more — working on similar project right now, though we haven't introduced events and transitions to the workflow yet

Upvotes: 0

Related Questions