Module: SolidQueueDashboard::PaginationHelper

Included in:
ApplicationController
Defined in:
app/helpers/solid_queue_dashboard/pagination_helper.rb

Instance Method Summary collapse

Instance Method Details

#page_range(current_page, total_pages, window: 2) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/solid_queue_dashboard/pagination_helper.rb', line 22

def page_range(current_page, total_pages, window: 2)
  if total_pages <= 7
    (1..total_pages).to_a
  else
    [
      1,
      (current_page - window..current_page + window).to_a,
      total_pages
    ].flatten.uniq.sort.reject { |p| p < 1 || p > total_pages }.tap do |range|
      range.each_cons(2) do |a, b|
        range.insert(range.index(b), :gap) if b - a > 1
      end
    end
  end
end

#paginate(scope, page:, per_page:) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/solid_queue_dashboard/pagination_helper.rb', line 3

def paginate(scope, page:, per_page:)
  page = [ page.to_i, 1 ].max
  per_page = per_page.zero? ? 25 : [ per_page.to_i, 1 ].max

  offset = (page - 1) * per_page

  records = scope.offset(offset).limit(per_page)
  total_count = scope.count
  total_pages = (total_count.to_f / per_page).ceil

  {
    records: records,
    current_page: page,
    per_page: per_page,
    total_pages: total_pages,
    total_count: total_count
  }
end