class Parelation::Applier

Constants

CRITERIA

@return [Array] the list of active criteria classes

that are used to narrow down database results.

Attributes

chain[R]

@return [ActiveRecord::Relation]

params[R]

@return [ActionController::Parameters, Hash]

Public Class Methods

new(chain, params) click to toggle source

@param chain [ActionController::Relation] the base chain to build on. @param params [ActionController::Parameters, Hash] user input via params.

# File lib/parelation/applier.rb, line 26
def initialize(chain, params)
  @chain = chain
  @params = params
end

Public Instance Methods

apply() click to toggle source

@return [ActiveRecord::Relation] the criteria-applied {#chain}.

# File lib/parelation/applier.rb, line 33
def apply
  @apply ||= apply_to_chain
end

Private Instance Methods

apply_to_chain() click to toggle source

Iterates over each user-provided parameter and incrementally updates the {#chain} to incorporate the user-requested criteria.

@return [ActiveRecord::Relation]

# File lib/parelation/applier.rb, line 44
def apply_to_chain
  params.each do |param, value|
    CRITERIA.each do |criteria|
      if criteria.match?(param)
        begin
          @chain = criteria.new(chain, param, value).call
        rescue
          raise Parelation::Errors::Parameter,
            "the #{param} parameter is invalid"
        end
      end
    end
  end

  chain
end