Skip to content

Class Methods for Transitions

CodeMeister edited this page Mar 8, 2023 · 1 revision

transition class methods:

  • #attr_transitions
    • Returns a Hash of all states and the transitions they are allowed.
  • #attr_transitions_for(state)
    • Returns an Array of all states the supplied state may transition to.

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.status_transitions #=> { draft:     [:pending],
                        #=>   pending:   [:draft, :published],
                        #=>   published: [:archived],
                        #=>   archived:  [] }

Post.status_transitions_for(:draft)    #=> [:pending]
Post.status_transitions_for(:pending)  #=> [:draft, :published]
Post.status_transitions_for(:archived) #=> []

Clone this wiki locally