Class: SourceMonitor::Pagination::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/pagination/paginator.rb

Constant Summary collapse

DEFAULT_PER_PAGE =
25

Instance Method Summary collapse

Constructor Details

#initialize(scope:, page: 1, per_page: DEFAULT_PER_PAGE) ⇒ Paginator

Returns a new instance of Paginator.



44
45
46
47
48
# File 'lib/source_monitor/pagination/paginator.rb', line 44

def initialize(scope:, page: 1, per_page: DEFAULT_PER_PAGE)
  @scope = scope
  @page = normalize_page(page)
  @per_page = normalize_per_page(per_page)
end

Instance Method Details

#paginateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/source_monitor/pagination/paginator.rb', line 50

def paginate
  total = compute_total_count
  paginated_records = fetch_records
  has_next_page = paginated_records.length > per_page

  Result.new(
    records: paginated_records.first(per_page),
    page: page,
    per_page: per_page,
    has_next_page: has_next_page,
    has_previous_page: page > 1,
    total_count: total
  )
end