Skip to content

Is there a way for the event methods to skip validations? #33

Description

@terenceponce

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions