Module: SesDashboard::Paginatable

Defined in:
lib/ses_dashboard/paginatable.rb

Overview

Lightweight pagination that avoids requiring kaminari or will_paginate. Returns [records, pagination_info] where pagination_info is a Hash.

Usage:

records, pagination = Paginatable.paginate(scope, page: 2, per_page: 25)

Class Method Summary collapse

Class Method Details

.paginate(scope, page:, per_page: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ses_dashboard/paginatable.rb', line 9

def self.paginate(scope, page:, per_page: nil)
  per_page    = (per_page || SesDashboard.configuration.per_page).to_i
  page        = [page.to_i, 1].max
  total_count = scope.count
  total_pages = [(total_count.to_f / per_page).ceil, 1].max
  page        = [page, total_pages].min

  records = scope.offset((page - 1) * per_page).limit(per_page)

  pagination = {
    page:        page,
    per_page:    per_page,
    total_count: total_count,
    total_pages: total_pages,
    prev_page:   page > 1 ? page - 1 : nil,
    next_page:   page < total_pages ? page + 1 : nil
  }

  [records, pagination]
end