module SnFoil::Controller::Concerns::IndexControllerConcern

Public Instance Methods

index(**options) click to toggle source
# File lib/sn_foil/controller/concerns/index_controller_concern.rb, line 16
def index(**options)
  options = setup_index(**options)
  results = process_index(**options)
  render_index(results, **options)
end
meta(results, **options) click to toggle source
# File lib/sn_foil/controller/concerns/index_controller_concern.rb, line 52
def meta(results, **options)
  results = paginate(results, **options)
  total_pages = results.respond_to?(:total_pages) ? results.total_pages : nil
  total_count = results.respond_to?(:total_count) ? results.total_count : nil

  {
    page: page(**options),
    pages: total_pages,
    total: total_count,
    per: per_page(**options)
  }
end
page(**options) click to toggle source
# File lib/sn_foil/controller/concerns/index_controller_concern.rb, line 41
def page(**options)
  (options.dig(:params, :page) || 1).to_i
end
paginate(results, **options) click to toggle source
# File lib/sn_foil/controller/concerns/index_controller_concern.rb, line 34
def paginate(results, **options)
  return results unless results.respond_to?(:page)

  results.page(page(**options))
         .per(per_page(**options))
end
per_page(**options) click to toggle source
# File lib/sn_foil/controller/concerns/index_controller_concern.rb, line 45
def per_page(**options)
  per_page_param = (options.dig(:params, :per_page) || 10).to_i
  return 1000 if per_page_param.zero? || per_page_param > 1000

  per_page_param
end
process_index(**options) click to toggle source
# File lib/sn_foil/controller/concerns/index_controller_concern.rb, line 26
def process_index(**options)
  current_context(**options).index(options)
end
render_index(results, **options) click to toggle source
# File lib/sn_foil/controller/concerns/index_controller_concern.rb, line 30
def render_index(results, **options)
  render paginate(results, **options), meta: meta(results, options)
end
setup_index(**options) click to toggle source
# File lib/sn_foil/controller/concerns/index_controller_concern.rb, line 22
def setup_index(**options)
  setup_options(**options)
end