module StateGate::Builder::StateMethods
Description¶ ↑
Multiple private methods enabling StateGate::Builder
to generate state functionality.
-
query the class for all state:
Klass.statuses # => [:pending, :active, :archived]
-
query the class for the human names of all state:
Klass.human_statuses # => ['Pending Activation', 'Active', 'Archived']
-
query the class for an Array of human names/state names for use in a select form:
Klass.statuses_for_select # => [['Pending Activation', 'pending'],["Active', 'active'], ['Archived','archived']]
-
list all attribute states:
.status_states # => [:pending, :active, :archived]
-
list all human names for the attribute states:
.status_human_names # => ['Pending Activation', 'Active', 'Archived']
-
list the human name for the attribute state:
.human_status # => 'Pending Activation'
-
is a particular state set:
.pending? # => false .active? # => true .archived? # => false
-
is a particular state not set:
.not_pending? # => true .not_active? # => false .not_archived? # => true
-
list the allowed transitions for the current state.
.status_transitions # => [:suspended, :archived]
Private Instance Methods
Adds an Instance method to return Array of the defined states for the attribute
eg: .statuses # => [:pending, :active, :suspended, :archived]
# File lib/state_gate/builder/state_methods.rb, line 169 def _add__instance__attrs(method_name = @attribute) method_name = method_name.to_s.pluralize add__instance__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name} stateables[:#{@attribute}].states end )) end
Adds a, Instance method to return an Array of the human and state names for the attribute, suitable for using in a form select statement.
sorted - if TRUE, the array is sorted in alphabetical order by human name
otherwise it is in the order specified .statuses_for_select # => [ ['Pending Activation', 'pending'], ['Active', 'active'], ['Suspended by Admin', 'suspended', ['Archived', 'archived'] ] .statuses_for_select(true) # => [ ['Active', 'active'], ['Pending Activation', 'pending'], ['Suspended by Admin', 'suspended', ['Archived', 'archived'] ]
Note:
States should NEVER be set from direct user selection. This method is intended for use within search forms, where the user may filter by state.
# File lib/state_gate/builder/state_methods.rb, line 277 def _add__instance__attrs_for_select(method_name = @attribute) method_name = "#{method_name.to_s.pluralize}_for_select" add__instance__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name}(sorted = false) stateables[:#{@attribute}].states_for_select(sorted) end )) end
Adds an Instance method to return the human name for the attribute’s state
eg: .human_status # => 'Suspended by Admin'
# File lib/state_gate/builder/state_methods.rb, line 202 def _add__instance__human_attr(method_name = @attribute) method_name = "human_#{method_name.to_s}" add__instance__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name} stateables[:#{@attribute}].human_state_for(#{@attribute}) end )) end
Adds an Instance method to return an Array of the human names for the attribute
eg: .status_human_states # => ['Pending Activation', 'Active', 'Suspended by Admin', 'Archived']
# File lib/state_gate/builder/state_methods.rb, line 186 def _add__instance__human_attrs(method_name = @attribute) method_name = "human_#{method_name.to_s.pluralize}" add__instance__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name} stateables[:#{@attribute}].human_states end )) end
Adds an Instance method for each state, returning TRUE if the state is not set.
eg: --> when :active .not_active? # => false .not_archived? # => true
def add_instance__not_state?
attr_name = @attribute
# File lib/state_gate/builder/state_methods.rb, line 243 def _add__instance__not_state? @engine.states.each do |state| method_name = "not_#{@engine.scope_name_for_state(state)}?" add__instance__helper_method(method_name, __FILE__, __LINE__ - 3, %( def #{method_name} self[:#{@attribute}] != :#{state}.to_s end )) end end
Adds an Instance method for each state, returning TRUE if the state is set
eg: --> when :active .active? # => true .archived? # => false
# File lib/state_gate/builder/state_methods.rb, line 220 def _add__instance__state? @engine.states.each do |state| method_name = "#{@engine.scope_name_for_state(state)}?" add__instance__helper_method(method_name, __FILE__, __LINE__ - 3, %( def #{method_name} self[:#{@attribute}] == :#{state}.to_s end )) end end
Adds a Class method to return an Array of the defined states for the attribute
eg: Klass.statuses # => [:pending, :active, :suspended, :archived]
# File lib/state_gate/builder/state_methods.rb, line 99 def _add__klass__attrs(method_name = @attribute) method_name = method_name.to_s.pluralize add__klass__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name} stateables[:#{@attribute}].states end )) end
Adds a Class method to return an Array of the human and state names for the attribute, suitable for using in a form select statement.
sorted - if TRUE, the array is sorted in alphabetical order by human name
otherwise it is in the order specified Klass.statuses_for_select # => [ ['Pending Activation', 'pending'], ['Active', 'active'], ['Suspended by Admin', 'suspended', ['Archived', 'archived'] ] Klass.statuses_for_select(true) # => [ ['Active', 'active'], ['Pending Activation', 'pending'], ['Suspended by Admin', 'suspended', ['Archived', 'archived'] ]
Note:
States should NEVER be set from direct user selection. This method is intended for use within search forms, where the user may filter by state.
# File lib/state_gate/builder/state_methods.rb, line 149 def _add__klass__attrs_for_select(method_name = @attribute) method_name = "#{method_name.to_s.pluralize}_for_select" add__klass__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name}(sorted = false) stateables[:#{@attribute}].states_for_select(sorted) end )) end
Adds a Class method to return an Array of the human names of the defined states for the attribute
eg: Klass.human_statuses # => ['Pending Activation', 'Active', 'Suspended by Admin', 'Archived']
# File lib/state_gate/builder/state_methods.rb, line 117 def _add__klass__human_attrs(method_name = @attribute) method_name = "human_#{method_name.to_s.pluralize}" add__klass__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name} stateables[:#{@attribute}].human_states end )) end
add alias methods
# File lib/state_gate/builder/state_methods.rb, line 77 def add_state_alias_methods return unless @alias _add__klass__attrs(@alias) _add__klass__human_attrs(@alias) _add__klass__attrs_for_select(@alias) _add__instance__attrs(@alias) _add__instance__human_attrs(@alias) _add__instance__attrs_for_select(@alias) end
add attribute methods
# File lib/state_gate/builder/state_methods.rb, line 60 def add_state_attribute_methods _add__klass__attrs _add__klass__human_attrs _add__klass__attrs_for_select _add__instance__attrs _add__instance__human_attrs _add__instance__human_attr _add__instance__state? _add__instance__not_state? _add__instance__attrs_for_select end
Add Class and instance methods that allow querying states
# File lib/state_gate/builder/state_methods.rb, line 51 def generate_state_methods add_state_attribute_methods add_state_alias_methods end