203 private links
How to recognize a state machine pattern?
- a
state
orstatus
boolean flag - boolean fields such as
published
orpaid
. Also timestamps that can have aNULL
value likepublished_at
- a record that is valid for a given period in time
Then the transition history has to be kept. At some point the transition history will be an invaluable source of information. The simplest way to keep track of the transitions is to add a timestamp field for every possible state. However, it is often possible to revisit the same state multiple times. In that case, simply adding fields to your model won’t do the trick because you will be overwriting them. Instead, add a log table in which all the state transitions will be logged. Fields that you probably want to include are the timestamp, the old state, the new state, and the event that caused the transition.
Instead of removing an object, add an error state for any reason you would have wanted to delete a record. A spam account? Don’t delete it, set it to the spam state. A fraudulent order? Don’t delete it, set it to the fraud state.