Skip to content

Creating a StateGate

CodeMeister edited this page Feb 28, 2023 · 10 revisions

step 1:

  • include StateGate within the model class.

step 2:

  • create the state-gate on the required attribute, defining the individual states and allowed transactions.

simple example:

This example creates a minimal state-gate on the :status attribute of the Post class, allowing any state to transition to any other state without restriction.

class Post < ActiveRecord::Base
  include StateGate

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

Post.new.status  #=> 'draft'

complex example:

This example creates a complex state-gate with a prefix, a suffix, no scopes and one-way, looping, sequential transitions.

class UKTrafficLight < ActiveRecord::Base
  include StateGate

  state_gate :status do
    state :green
    state :yellow
    state :red
    state :red_and_yellow

    default :red

    prefix :traffic
    suffix :light

    no_scopes

    make_sequential :one_way, :loop
  end
end

Clone this wiki locally