-
Notifications
You must be signed in to change notification settings - Fork 1
Instance Methods for States
CodeMeister edited this page Feb 28, 2023
·
1 revision
-
.attrs
- Returns an Array of the Symbol names for each state, in the order each state was added.
-
.human_attrs
- Returns an Array of the String display names for each state, in the order each state was defined.
-
.human_attr
- Returns a String with the display name for the attributes current state.
-
.attrs_for_select
- This method returns an Array of Arrays, each containing both the String display name
and Symbol name for each state, suitable for use in a form
select
field. - By default, results are in the order that each state was added, but passing
:sorted
will re-order them alphabetically by human name.
- This method returns an Array of Arrays, each containing both the String display name
and Symbol name for each state, suitable for use in a form
-
.state?
- Each state has a predicate method that will return
true
if the state is set, andfalse
if not.
- Each state has a predicate method that will return
-
.not_state?
- Each state has a predicate method that will return
true
if the state is not set, andfalse
if it is.
- Each state has a predicate method that will return
class Post < ActiveRecord::Base
include StateGate
state_gate :status do
state :draft
state :pending, human: 'Pending Approval'
state :published
state :archived
end
end
Post.new.statuses #=> [:draft, :pending, :published, :archived]
Post.new.human_statuses #=> ['Draft', 'Pending Activation', 'Published', 'Archived']
post.status = :pending #=> true
post.human_status #=> 'Pending Approval'
post.statuses_for_select #=> [ ['Draft', :draft],
#=> ['Pending Activation', :pending],
#=> ['Published', :published],
#=> ['Archived', :archived] ]
post.statuses_for_select(:sorted) #=> [ ['Archived', :archived] ],
#=> ['Draft', :draft],
#=> ['Pending Activation', :pending],
#=> ['Published', :published] ]
post.draft? #=> false
post.pending? #=> true
post.published? #=> false
post.archived? #=> false
post.not_draft? #=> true
post.not_pending? #=> false
post.not_published? #=> true
post.not_archived? #=> true