Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ordered states #330

Open
neutralino1 opened this issue Nov 10, 2014 · 2 comments
Open

ordered states #330

neutralino1 opened this issue Nov 10, 2014 · 2 comments

Comments

@neutralino1
Copy link

Hi all,

If the states of your machine represent successive states instead of independent states (e.g. new -> pending -> ongoing -> completed), it is useful to know if a particular state has been passed. For example, if you want to know if a state has passed the pending state, you need to check if it is either ongoing or completed. This is not really maintainable if you have 10 or so states.
The following pattern would be really useful.

model = Model.new
model.state > :ongoing
=> false
model.queue!
model.state > :new
=> true
model.start!
model.state > :pending
=> true

Is there currently a generic way to achieve this?

@PhilT
Copy link

PhilT commented Dec 3, 2014

I was looking for a way to do something similar too. Storing the states as numeric values in the database could be a way to do this. This would also allow you to order queries by progressive state. I saw someone mentioned it's possible to store states as enums in PostgreSQL using the Statesman gem.

I'm about to investigate this myself. However, I'm not too keen on the Statesman gem as a whole and may come back to state_machine but I'm worried about a lack of a maintainer.

@PhilT
Copy link

PhilT commented Dec 3, 2014

Maybe you could implement this in your model with something like:

model.past(:ongoing)

And put the states in an array and do states.index(self.state) > states.index(state)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants