Skip to content

Unrestricted Transitions

CodeMeister edited this page Feb 28, 2023 · 3 revisions

...with unrestricted transitions:

When only states and no transitions are defined, then each state can transition to any other state without restriction.

With unrestricted transitions, it behaves like an improved eNum.

example:

class Post < ActiveRecord::Base
  include StateGate

  state_gate :status do
    state :draft
    state :pending
    state :published
    state :archived
  end
end

my_post = Post.new
my_post.status                      #=> 'draft'
my_post.update(status: :published)  #=> 'published'
my_post.status = 'archived'         #=> 'archived'
my_post.status = :draft             #=> 'draft'
my_post.status = :revoked           #=> <ArgumentError>

Clone this wiki locally