class AbstractFinder

AbstractFinder

Constants

SORT

Attributes

associations[R]
model[R]

Public Class Methods

call(model, associations = [], args = []) click to toggle source
# File lib/abstract_finder.rb, line 9
def self.call(model, associations = [], args = [])
  new(model, associations, args).call
end
new(model, associations, args) click to toggle source
# File lib/abstract_finder.rb, line 13
def initialize(model, associations, args) # rubocop:disable Lint/MissingSuper
  @model = model
  @associations = associations
  @filters = args
end

Public Instance Methods

call() click to toggle source
# File lib/abstract_finder.rb, line 19
def call
  load_collection

  self
end

Protected Instance Methods

base_collection() click to toggle source
# File lib/abstract_finder.rb, line 36
def base_collection
  @collection = model.all.includes(associations)
end
filter() click to toggle source
# File lib/abstract_finder.rb, line 49
def filter; end
finalize_collection() click to toggle source
# File lib/abstract_finder.rb, line 40
def finalize_collection
  @collection = @collection.page(page).per(per_page)
end
load_collection() click to toggle source
# File lib/abstract_finder.rb, line 27
def load_collection
  base_collection
  query if @filters[:q].present? && @collection.respond_to?(:search_by)
  order if @filters[:order].present?
  filter if @filters[:filter_by].present? && @collection.respond_to?(:filter_by)
  total
  finalize_collection
end
order() click to toggle source
# File lib/abstract_finder.rb, line 51
def order
  @collection = @collection.order(created_at: @filters[:order])
end
query() click to toggle source

define search_by method in query model and use this finder

# File lib/abstract_finder.rb, line 45
def query
  @collection = @collection.includes(associations).search_by(@filters[:q]) if @collection.respond_to? :search_by
end