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

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

Open
terenceponce opened this issue May 11, 2018 · 3 comments
Open

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

terenceponce opened this issue May 11, 2018 · 3 comments

Comments

@terenceponce
Copy link

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?

@amatsuda
Copy link
Owner

Interesting idea. I’ve never thought of that, but suggestions with implementations are welcome!

@amatsuda
Copy link
Owner

amatsuda commented May 11, 2018

Sorry, just closed the issue by accident, but I’ll leave this opened until someone actually implements this.

@amatsuda amatsuda reopened this May 11, 2018
@jairovm
Copy link

jairovm commented Apr 14, 2020

Not exactly what you're trying to achive @terenceponce, but I prevent records from transitioning to an invalid state through #create or #update methods in this PR #49

amatsuda added a commit that referenced this issue Aug 7, 2021
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

3 participants