module SnFoil::Searcher

Constants

ASC
DESC

Public Instance Methods

distinct(bool = true) click to toggle source
# File lib/sn_foil/searcher.rb, line 37
def distinct(bool = true) # rubocop:disable Style/OptionalBooleanParameter reason: class configuration looks better this way
  @i_is_distinct = bool
end
distinct?() click to toggle source
# File lib/sn_foil/searcher.rb, line 47
def distinct?
  self.class.i_is_distinct || false
end
included_params() click to toggle source
# File lib/sn_foil/searcher.rb, line 51
def included_params
  self.class.i_include_params
end
includes(*array) click to toggle source
# File lib/sn_foil/searcher.rb, line 41
def includes(*array)
  @i_include_params ||= [] # create new array if none exists
  @i_include_params |= array # combine unique elements of both arrays
end
order(method = nil, &block) click to toggle source
# File lib/sn_foil/searcher.rb, line 27
def order(method = nil, &block)
  @i_order_method = method
  @i_order_block = block
end
order_by(attr, direction = nil) click to toggle source
# File lib/sn_foil/searcher.rb, line 32
def order_by(attr, direction = nil)
  @i_order_by_attr = attr
  @i_order_by_direction = direction
end

Private Instance Methods

apply_distinct(filtered_scope, params) click to toggle source
# File lib/sn_foil/searcher.rb, line 114
def apply_distinct(filtered_scope, params)
  return filtered_scope unless distinct? || params[:distinct] == true

  filtered_scope.distinct
end
apply_includes(filtered_scope) click to toggle source
# File lib/sn_foil/searcher.rb, line 81
def apply_includes(filtered_scope)
  return filtered_scope unless included_params

  filtered_scope.includes(*included_params)
end
apply_order(filtered_scope, params) click to toggle source
# File lib/sn_foil/searcher.rb, line 87
def apply_order(filtered_scope, params)
  return order_method(filtered_scope, params) if order_method?
  return order_block(filtered_scope, params) if order_block?

  if params[:order_by].blank? && params[:order].blank?
    filtered_scope.order(order_by => order)
  else
    filtered_scope.order(order_by(params) => order(params))
  end
end
order_block(filtered_scope, params) click to toggle source
# File lib/sn_foil/searcher.rb, line 106
def order_block(filtered_scope, params)
  instance_exec filtered_scope, params, &self.class.i_order_block
end
order_block?() click to toggle source
# File lib/sn_foil/searcher.rb, line 110
def order_block?
  self.class.i_order_block.present?
end
order_method(filtered_scope, params) click to toggle source
# File lib/sn_foil/searcher.rb, line 98
def order_method(filtered_scope, params)
  send(self.class.i_order_method, filtered_scope, params)
end
order_method?() click to toggle source
# File lib/sn_foil/searcher.rb, line 102
def order_method?
  self.class.i_order_method.present?
end