Files
danbooru/app/components/paginator_component.rb
evazion 55d00fc40c paginator: fix showing page 5000 when page count is unknown
Fix a bug where if you did a slow search that took too long to calculate
the page count, and you had 200 posts per page, then we would show page
5000 as the last page of the search.

This was because we were artificially returning 1,000,000 as the post
count to signal that the count timed out, but at 200 posts per page this
would show 5000 as the last page of the search.
2021-09-08 18:33:28 -05:00

47 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class PaginatorComponent < ApplicationComponent
attr_reader :records, :window, :params
delegate :current_page, :prev_page, :next_page, :total_pages, :paginator_mode, :paginator_page_limit, to: :records
delegate :ellipsis_icon, :chevron_left_icon, :chevron_right_icon, to: :helpers
def initialize(records:, params:, window: 4)
super
@records = records
@window = window
@params = params
end
def use_sequential_paginator?
paginator_mode != :numbered
end
def pages
last_page = total_pages.clamp(1..)
left = (current_page - window).clamp(2..)
right = (current_page + window).clamp(..last_page - 1)
[
1,
("..." unless left == 2),
(left..right).to_a,
("..." unless right == last_page - 1),
(last_page unless last_page == 1 || last_page.infinite?),
].flatten.compact
end
def link_to_page(anchor, page = anchor, **options)
if page.nil?
tag.span anchor, **options
else
hidden = paginator_mode == :numbered && page > paginator_page_limit
link_to anchor, url_for_page(page), **options, hidden: hidden
end
end
def url_for_page(page)
url_for(**params.merge(page: page).permit!)
end
end