Skip to content

What is a Transition

CodeMeister edited this page Mar 8, 2023 · 3 revisions

Changing from one state to another is called a transition. The transitions_to option limits which other states each individual state may transition to.

A state can can transition to another single state, one of many states, or not transition at all

If an invalid state is requested, then an ArgumentError is raised.

example:

class Post < ActiveRecord::Base
  include StateGate

  state_gate :status do
    state :draft,     transitions_to: :pending
    state :pending,   transitions_to: [:published, :draft]
    state :published, transitions_to: :archived
    state :archived
  end
end

post = Post.new
post.status                 #=> 'draft'
post.status = :pending      #=> 'pending'
post.status = :draft        #=> 'draft'
post.status = :pending      #=> 'pending'
post.status = :published    #=> 'published'
post.status = 'archived'    #=> 'archived'
post.status = :published    #=> <ArgumentError>

Clone this wiki locally