I want any state changes to exclusively go through the state machine and prevent other developers, including myself, to change the state directly via #create or #update. Here's an example:
class User < ApplicationRecord
enum status: [:active, :inactive, :banned] do
event :deactivate do
transition :active => :inactive
end
event :ban do
transition :active => :banned
end
end
validates :status, inclusion: { in: %w(active) }, on: :create
validates :status, absence: true, on: :update
end
As you can see, the validations make sure that the following code won't work:
User.create(state: "inactive")
User.create(state: "banned")
user.update(state: "inactive")
user.update(state: "banned")
Unfortunately, the event methods provided by stateful_enum won't work because it will have to go through the same validations. Is there a way for me to skip these validations when the state change comes from stateful_enum?
I want any state changes to exclusively go through the state machine and prevent other developers, including myself, to change the state directly via
#createor#update. Here's an example:As you can see, the validations make sure that the following code won't work:
Unfortunately, the event methods provided by
stateful_enumwon't work because it will have to go through the same validations. Is there a way for me to skip these validations when the state change comes fromstateful_enum?