Module: Postnhost::PagyHelper

Defined in:
app/helpers/postnhost/pagy_helper.rb

Constant Summary collapse

DEFAULT_PAGINATION_SLOTS =
7

Instance Method Summary collapse

Instance Method Details

#pagination_nav(pagy) ⇒ Object



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

def pagination_nav(pagy)
  return "".html_safe if pagy.pages <= 1

  (:nav,
              class: "flex items-center justify-center space-x-2",
              aria: { label: I18n.t("postnhost.public.pagination.navigation", default: "Pagination") }) do
    safe_join(
      [
        pagination_previous_link(pagy),
        *pagination_series_fragments(pagy),
        pagination_next_link(pagy)
      ],
      ""
    )
  end
end

#pagination_series(pagy) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/postnhost/pagy_helper.rb', line 27

def pagination_series(pagy)
  slots = pagy.options.fetch(:slots, DEFAULT_PAGINATION_SLOTS)
  return [] if slots.zero?
  return (1..pagy.pages).to_a if slots >= pagy.pages

  half = (slots - 1) / 2
  start = if pagy.page <= half
            1
          elsif pagy.page > pagy.pages - slots + half
            pagy.pages - slots + 1
          else
            pagy.page - half
          end

  (start...(start + slots)).to_a.tap do |series|
    next if pagy.options[:compact] || slots < DEFAULT_PAGINATION_SLOTS

    series[0] = 1
    series[1] = :gap unless series[1] == 2
    series[-2] = :gap unless series[-2] == pagy.pages - 1
    series[-1] = pagy.pages
  end
end

#pagination_url(pagy, page) ⇒ Object

Keep first pages canonical while preserving unrelated query parameters.



23
24
25
# File 'app/helpers/postnhost/pagy_helper.rb', line 23

def pagination_url(pagy, page)
  pagy.page_url(page == 1 ? :first : page)
end