module StateGate::Builder::ScopeMethods

Description

Multiple private methods enabling StateGate::Builder to generate scopes for each state.

Private Instance Methods

_add__klass__not_state_scopes() click to toggle source

Add a klass method that scopes records to those without the specified state.

eg:
    Klass.not_active         # => ActiveRecord::Relation
    Klass.not_active_status  # => ActiveRecord::Relation
# File lib/state_gate/builder/scope_methods.rb, line 70
def _add__klass__not_state_scopes
  attr_name = @attribute

  @engine.states.each do |state|
    scope_name = @engine.scope_name_for_state(state)
    detect_class_method_conflict! "not_#{scope_name}"
    @klass.scope "not_#{scope_name}", -> { where.not(attr_name => state) }
  end # each state
end
_add__klass__state_scopes() click to toggle source

Add a klass method that scopes records to the specified state.

eg:
    Klass.active         # => ActiveRecord::Relation
    Klass.active_status  # => ActiveRecord::Relation
# File lib/state_gate/builder/scope_methods.rb, line 53
def _add__klass__state_scopes
  attr_name = @attribute

  @engine.states.each do |state|
    scope_name = @engine.scope_name_for_state(state)
    detect_class_method_conflict! scope_name
    @klass.scope(scope_name, -> { where(attr_name => state) })
  end # each state
end
_add__klass__with_attrs_scope(method_name = @attribute) click to toggle source

Add a klass method that scopes records to the given states.

eg:
    Klass.with_statuses(:active, :pending) # => ActiveRecord::Relation
# File lib/state_gate/builder/scope_methods.rb, line 86
def _add__klass__with_attrs_scope(method_name = @attribute)
  attr_name   = @attribute
  method_name = "with_#{method_name.to_s.pluralize}"

  detect_class_method_conflict! method_name
  @klass.scope method_name, ->(states) { where(attr_name => Array(states)) }
end
generate_scope_methods() click to toggle source

Add scopes to the klass for filtering by state

Note:

The scope name is a concatenation of <prefix><state name><suffix>
# File lib/state_gate/builder/scope_methods.rb, line 32
def generate_scope_methods
  return unless @engine.include_scopes?

  _add__klass__state_scopes
  _add__klass__not_state_scopes
  _add__klass__with_attrs_scope

  _add__klass__with_attrs_scope(@alias) if @alias
end