pagy 6.5.0 → 7.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/lib/config/pagy.rb +18 -16
  4. data/lib/javascripts/pagy-dev.js +10 -11
  5. data/lib/javascripts/pagy-module.js +9 -10
  6. data/lib/javascripts/pagy.js +1 -1
  7. data/lib/locales/ar.yml +14 -10
  8. data/lib/locales/be.yml +11 -10
  9. data/lib/locales/bg.yml +9 -10
  10. data/lib/locales/bs.yml +11 -10
  11. data/lib/locales/ca.yml +11 -10
  12. data/lib/locales/ckb.yml +8 -10
  13. data/lib/locales/cs.yml +13 -12
  14. data/lib/locales/da.yml +11 -10
  15. data/lib/locales/de.yml +9 -10
  16. data/lib/locales/en.yml +9 -10
  17. data/lib/locales/es.yml +9 -10
  18. data/lib/locales/fr.yml +9 -10
  19. data/lib/locales/hr.yml +11 -10
  20. data/lib/locales/id.yml +9 -10
  21. data/lib/locales/it.yml +9 -10
  22. data/lib/locales/ja.yml +9 -10
  23. data/lib/locales/km.yml +9 -9
  24. data/lib/locales/ko.yml +9 -10
  25. data/lib/locales/nb.yml +9 -10
  26. data/lib/locales/nl.yml +9 -10
  27. data/lib/locales/nn.yml +9 -10
  28. data/lib/locales/pl.yml +11 -10
  29. data/lib/locales/pt-BR.yml +9 -10
  30. data/lib/locales/pt.yml +9 -10
  31. data/lib/locales/ru.yml +13 -10
  32. data/lib/locales/sr.yml +11 -9
  33. data/lib/locales/sv-SE.yml +10 -12
  34. data/lib/locales/sv.yml +10 -12
  35. data/lib/locales/sw.yml +11 -10
  36. data/lib/locales/ta.yml +11 -10
  37. data/lib/locales/tr.yml +9 -10
  38. data/lib/locales/uk.yml +11 -10
  39. data/lib/locales/vi.yml +7 -10
  40. data/lib/locales/zh-CN.yml +9 -10
  41. data/lib/locales/zh-HK.yml +9 -10
  42. data/lib/locales/zh-TW.yml +9 -10
  43. data/lib/pagy/backend.rb +3 -2
  44. data/lib/pagy/calendar/day.rb +3 -2
  45. data/lib/pagy/calendar/helper.rb +3 -7
  46. data/lib/pagy/calendar/month.rb +3 -2
  47. data/lib/pagy/calendar/quarter.rb +3 -2
  48. data/lib/pagy/calendar/year.rb +2 -1
  49. data/lib/pagy/extras/bootstrap.rb +45 -30
  50. data/lib/pagy/extras/bulma.rb +44 -33
  51. data/lib/pagy/extras/calendar.rb +7 -8
  52. data/lib/pagy/extras/elasticsearch_rails.rb +6 -6
  53. data/lib/pagy/extras/foundation.rb +46 -33
  54. data/lib/pagy/extras/frontend_helpers.rb +8 -8
  55. data/lib/pagy/extras/headers.rb +9 -9
  56. data/lib/pagy/extras/i18n.rb +4 -4
  57. data/lib/pagy/extras/items.rb +11 -10
  58. data/lib/pagy/extras/jsonapi.rb +2 -7
  59. data/lib/pagy/extras/materialize.rb +41 -29
  60. data/lib/pagy/extras/meilisearch.rb +7 -7
  61. data/lib/pagy/extras/metadata.rb +6 -6
  62. data/lib/pagy/extras/navs.rb +26 -38
  63. data/lib/pagy/extras/overflow.rb +7 -5
  64. data/lib/pagy/extras/searchkick.rb +6 -6
  65. data/lib/pagy/extras/semantic.rb +40 -27
  66. data/lib/pagy/extras/standalone.rb +4 -7
  67. data/lib/pagy/extras/support.rb +18 -32
  68. data/lib/pagy/extras/trim.rb +2 -2
  69. data/lib/pagy/extras/uikit.rb +44 -31
  70. data/lib/pagy/frontend.rb +59 -28
  71. data/lib/pagy/i18n.rb +0 -2
  72. data/lib/pagy/url_helpers.rb +1 -2
  73. data/lib/pagy.rb +16 -14
  74. data/lib/stylesheets/pagy.css +61 -0
  75. data/lib/stylesheets/pagy.scss +50 -0
  76. data/lib/stylesheets/pagy.tailwind.scss +24 -0
  77. metadata +7 -19
  78. data/lib/templates/bootstrap_nav.html.erb +0 -24
  79. data/lib/templates/bootstrap_nav.html.haml +0 -34
  80. data/lib/templates/bootstrap_nav.html.slim +0 -34
  81. data/lib/templates/bulma_nav.html.erb +0 -24
  82. data/lib/templates/bulma_nav.html.haml +0 -32
  83. data/lib/templates/bulma_nav.html.slim +0 -32
  84. data/lib/templates/foundation_nav.html.erb +0 -24
  85. data/lib/templates/foundation_nav.html.haml +0 -34
  86. data/lib/templates/foundation_nav.html.slim +0 -34
  87. data/lib/templates/nav.html.erb +0 -22
  88. data/lib/templates/nav.html.haml +0 -30
  89. data/lib/templates/nav.html.slim +0 -29
  90. data/lib/templates/uikit_nav.html.erb +0 -15
  91. data/lib/templates/uikit_nav.html.haml +0 -28
  92. data/lib/templates/uikit_nav.html.slim +0 -28
data/lib/locales/uk.yml CHANGED
@@ -1,24 +1,25 @@
1
1
  # :east_slavic pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
2
-
3
2
  uk:
4
3
  pagy:
5
-
4
+ aria_label:
5
+ nav:
6
+ one: "Сторінка"
7
+ few: "Сторінки"
8
+ many: "Сторінок"
9
+ other: "Сторінки"
10
+ prev: "Назад"
11
+ next: "Далі"
12
+ prev: "<"
13
+ next: ">"
14
+ gap: "…"
6
15
  item_name:
7
16
  one: "запис"
8
17
  few: "записів"
9
18
  many: "записів"
10
19
  other: "записів"
11
-
12
- nav:
13
- prev: "‹ Назад"
14
- next: "Далі ›"
15
- gap: "…"
16
-
17
20
  info:
18
21
  no_items: "Більше %{item_name} немає"
19
22
  single_page: "<b>%{count}</b> %{item_name}"
20
23
  multiple_pages: "Загалом <b>%{count}</b> %{item_name}, показані з <b>%{from}</b> по <b>%{to}</b>"
21
-
22
24
  combo_nav_js: "<label>Сторінка %{page_input} з %{pages}</label>"
23
-
24
25
  items_selector_js: "<label>Показати %{items_input} %{item_name} на сторінці</label>"
data/lib/locales/vi.yml CHANGED
@@ -1,20 +1,17 @@
1
1
  # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
2
-
3
2
  vi:
4
3
  pagy:
5
-
4
+ aria_label:
5
+ nav: "Trang"
6
+ prev: "Trang trước"
7
+ next: "Trang sau"
8
+ prev: "&lt;"
9
+ next: "&gt;"
10
+ gap: "&hellip;"
6
11
  item_name: "kết quả"
7
-
8
- nav:
9
- prev: "&lsaquo;&nbsp;Trang trước"
10
- next: "Trang sau&nbsp;&rsaquo;"
11
- gap: "&hellip;"
12
-
13
12
  info:
14
13
  no_items: "Không tìm thấy %{item_name} nào"
15
14
  single_page: "<b>%{count}</b> %{item_name}"
16
15
  multiple_pages: "Đang hiển thị <b>%{from}-%{to}</b> trong tổng số <b>%{count} %{item_name}</b>"
17
-
18
16
  combo_nav_js: "<label>Trang %{page_input} trên %{pages}</label>"
19
-
20
17
  items_selector_js: "<label>Hiển thị %{items_input} %{item_name} mỗi trang</label>"
@@ -1,20 +1,19 @@
1
1
  # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
2
-
3
2
  zh-CN:
4
3
  pagy:
5
-
4
+ aria_label:
5
+ # please add a comment in the https://github.com/ddnexus/pagy/issues/608
6
+ # posting the translation of the following "Page"/"Pages" with the plurals for this locale
7
+ nav: "Pages"
8
+ prev: "上一页"
9
+ next: "下一页"
10
+ prev: "&lt;"
11
+ next: "&gt;"
12
+ gap: "&hellip;"
6
13
  item_name: ""
7
-
8
- nav:
9
- prev: "&lsaquo;&nbsp;上一页"
10
- next: "下一页&nbsp;&rsaquo;"
11
- gap: "&hellip;"
12
-
13
14
  info:
14
15
  no_items: "项目未找到"
15
16
  single_page: "显示<b>%{count}</b> 项%{item_name}"
16
17
  multiple_pages: "共 <b>%{count}</b> 项%{item_name},显示 <b>%{from}-%{to}</b>"
17
-
18
18
  combo_nav_js: "<label>第 %{page_input} / %{pages} 页</label>"
19
-
20
19
  items_selector_js: "<label>每页显示 %{items_input} 条%</label>"
@@ -1,20 +1,19 @@
1
1
  # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
2
-
3
2
  zh-HK:
4
3
  pagy:
5
-
4
+ aria_label:
5
+ # please add a comment in the https://github.com/ddnexus/pagy/issues/609
6
+ # posting the translation of the following "Page"/"Pages" with the plurals for this locale
7
+ nav: "Pages"
8
+ prev: "上一頁"
9
+ next: "下一頁"
10
+ prev: "&lt;"
11
+ next: "&gt;"
12
+ gap: "&hellip;"
6
13
  item_name: ""
7
-
8
- nav:
9
- prev: "&lsaquo;&nbsp;上一頁"
10
- next: "下一頁&nbsp;&rsaquo;"
11
- gap: "&hellip;"
12
-
13
14
  info:
14
15
  no_items: "記錄未搵到"
15
16
  single_page: "顯示 <b>%{count}</b> %{item_name}"
16
17
  multiple_pages: "共 <b>%{count}</b> %{item_name},顯示 <b>%{from}-%{to}</b>"
17
-
18
18
  combo_nav_js: "<label>第 %{page_input} / %{pages} 頁</label>"
19
-
20
19
  items_selector_js: "<label>每頁顯示 %{items_input} 記錄</label>"
@@ -1,20 +1,19 @@
1
1
  # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
2
-
3
2
  zh-TW:
4
3
  pagy:
5
-
4
+ aria_label:
5
+ # please add a comment in the https://github.com/ddnexus/pagy/issues/610
6
+ # posting the translation of the following "Page"/"Pages" with the plurals for this locale
7
+ nav: "Pages"
8
+ prev: "上一頁"
9
+ next: "下一頁"
10
+ prev: "&lt;"
11
+ next: "&gt;"
12
+ gap: "&hellip;"
6
13
  item_name: ""
7
-
8
- nav:
9
- prev: "&lsaquo;&nbsp;上一頁"
10
- next: "下一頁&nbsp;&rsaquo;"
11
- gap: "&hellip;"
12
-
13
14
  info:
14
15
  no_items: "沒找到項"
15
16
  single_page: "顯示 <b>%{count}</b> 項%{item_name}"
16
17
  multiple_pages: "共 <b>%{count}</b> 項%{item_name},顯示 <b>%{from}-%{to}</b>"
17
-
18
18
  combo_nav_js: "<label>第 %{page_input} / %{pages} 頁</label>"
19
-
20
19
  items_selector_js: "<label>每頁顯示 %{items_input} 項%{item_name}</label>"
data/lib/pagy/backend.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class Pagy
5
- # Define a few generic methods to paginate an ORM collection out of the box,
5
+ # Define a few generic methods to paginate a collection out of the box,
6
6
  # or any collection by overriding pagy_get_items and/or pagy_get_vars in your controller
7
7
  # See also the extras if you need specialized methods to paginate Arrays or other collections
8
8
  module Backend
@@ -18,7 +18,8 @@ class Pagy
18
18
  # You may need to override the count call for non AR collections
19
19
  def pagy_get_vars(collection, vars)
20
20
  pagy_set_items_from_params(vars) if defined?(ItemsExtra)
21
- vars[:count] ||= (count = collection.count(:all)).is_a?(Hash) ? count.size : count
21
+ count_args = vars[:count_args] || DEFAULT[:count_args]
22
+ vars[:count] ||= (count = collection.count(*count_args)).is_a?(Hash) ? count.size : count
22
23
  vars[:page] ||= pagy_get_page(vars)
23
24
  vars
24
25
  end
@@ -5,8 +5,9 @@ class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
6
  # Calendar day subclass
7
7
  class Day < Calendar
8
- DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
- format: '%Y-%m-%d' }
8
+ DEFAULT = { size: 31, # rubocop:disable Style/MutableConstant
9
+ order: :asc,
10
+ format: '%d' }
10
11
 
11
12
  protected
12
13
 
@@ -17,13 +17,13 @@ class Pagy # :nodoc:
17
17
 
18
18
  # Create the calendar
19
19
  def init(conf, period, params)
20
- @units = Calendar::UNITS & conf.keys # get the units in time length desc order
20
+ @conf = Marshal.load(Marshal.dump(conf)) # store a copy
21
+ @units = Calendar::UNITS & @conf.keys # get the units in time length desc order
21
22
  raise ArgumentError, 'no calendar unit found in pagy_calendar @configuration' if @units.empty?
22
23
 
23
24
  @period = period
24
25
  @params = params
25
26
  @page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
26
- @conf = Marshal.load(Marshal.dump(conf)) # store a copy
27
27
  @units.each do |unit| # set all the :page_param vars for later deletion
28
28
  unit_page_param = :"#{unit}_#{@page_param}"
29
29
  conf[unit][:page_param] = unit_page_param
@@ -33,11 +33,7 @@ class Pagy # :nodoc:
33
33
  object = nil
34
34
  @units.each_with_index do |unit, index|
35
35
  params_to_delete = @units[(index + 1), @units.size].map { |sub| conf[sub][:page_param] } + [@page_param]
36
- conf[unit][:params] = lambda do |unit_params| # delete page_param from the sub-units
37
- # Hash#except missing from ruby 2.5 baseline
38
- params_to_delete.each { |p| unit_params.delete(p.to_s) }
39
- unit_params
40
- end
36
+ conf[unit][:params] = lambda { |up| up.except(*params_to_delete.map(&:to_s)) } # rubocop:disable Style/Lambda
41
37
  conf[unit][:period] = object&.send(:active_period) || @period
42
38
  calendar[unit] = object = Calendar.send(:create, unit, conf[unit])
43
39
  end
@@ -5,8 +5,9 @@ class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
6
  # Calendar month subclass
7
7
  class Month < Calendar
8
- DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
- format: '%Y-%m' }
8
+ DEFAULT = { size: 12, # rubocop:disable Style/MutableConstant
9
+ order: :asc,
10
+ format: '%b' }
10
11
 
11
12
  protected
12
13
 
@@ -5,8 +5,9 @@ class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
6
  # Calendar quarter subclass
7
7
  class Quarter < Calendar
8
- DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
- format: '%Y-Q%q' } # '%q' token
8
+ DEFAULT = { size: 4, # rubocop:disable Style/MutableConstant
9
+ order: :asc,
10
+ format: 'Q%q' } # '%q' token
10
11
 
11
12
  # The label for any page, with the substitution of the '%q' token
12
13
  def label_for(page, opts = {})
@@ -5,7 +5,8 @@ class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
6
  # Calendar year subclass
7
7
  class Year < Calendar
8
- DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
8
+ DEFAULT = { size: 10, # rubocop:disable Style/MutableConstant
9
+ order: :asc,
9
10
  format: '%Y' }
10
11
 
11
12
  protected
@@ -8,84 +8,99 @@ class Pagy # :nodoc:
8
8
  # The resulting code may not look very elegant, but produces the best benchmarks
9
9
  module BootstrapExtra
10
10
  # Pagination for bootstrap: it returns the html with the series of links to the pages
11
- def pagy_bootstrap_nav(pagy, pagy_id: nil, link_extra: '', **vars)
11
+ def pagy_bootstrap_nav(pagy, pagy_id: nil, link_extra: '',
12
+ nav_aria_label: nil, nav_i18n_key: nil, **vars)
12
13
  p_id = %( id="#{pagy_id}") if pagy_id
13
14
  link = pagy_link_proc(pagy, link_extra: %(class="page-link" #{link_extra}))
14
15
 
15
- html = +%(<nav#{p_id} class="pagy-bootstrap-nav"><ul class="pagination">)
16
- html << pagy_bootstrap_prev_html(pagy, link)
16
+ html = +%(<nav#{p_id} class="pagy-bootstrap-nav" #{
17
+ nav_aria_label_attr(pagy, nav_aria_label, nav_i18n_key)}><ul class="pagination">)
18
+ html << bootstrap_prev_html(pagy, link)
17
19
  pagy.series(**vars).each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
18
20
  html << case item
19
21
  when Integer
20
- %(<li class="page-item">#{link.call item}</li>)
22
+ %(<li class="page-item">#{link.call(item)}</li>)
21
23
  when String
22
- %(<li class="page-item active">#{link.call item}</li>)
24
+ %(<li class="page-item active"><a role="link" class="page-link" aria-current="page" aria-disabled="true">#{
25
+ pagy.label_for(item)}</a></li>)
23
26
  when :gap
24
- %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>)
27
+ %(<li class="page-item gap disabled"><a role="link" class="page-link" aria-disabled="true">#{
28
+ pagy_t 'pagy.gap'}</a></li>)
25
29
  else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
26
30
  end
27
31
  end
28
- html << pagy_bootstrap_next_html(pagy, link)
32
+ html << bootstrap_next_html(pagy, link)
29
33
  html << %(</ul></nav>)
30
34
  end
31
35
 
32
36
  # Javascript pagination for bootstrap: it returns a nav and a JSON tag used by the pagy.js file
33
- def pagy_bootstrap_nav_js(pagy, pagy_id: nil, link_extra: '', **vars)
37
+ def pagy_bootstrap_nav_js(pagy, pagy_id: nil, link_extra: '',
38
+ nav_aria_label: nil, nav_i18n_key: nil, **vars)
34
39
  sequels = pagy.sequels(**vars)
35
40
  p_id = %( id="#{pagy_id}") if pagy_id
36
41
  link = pagy_link_proc(pagy, link_extra: %(class="page-link" #{link_extra}))
37
- tags = { 'before' => %(<ul class="pagination">#{pagy_bootstrap_prev_html pagy, link}),
38
- 'link' => %(<li class="page-item">#{mark = link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER)}</li>),
39
- 'active' => %(<li class="page-item active">#{mark}</li>),
40
- 'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>),
41
- 'after' => %(#{pagy_bootstrap_next_html pagy, link}</ul>) }
42
+ tags = { 'before' => %(<ul class="pagination">#{bootstrap_prev_html pagy, link}),
43
+ 'link' => %(<li class="page-item">#{link.call(PAGE_TOKEN, LABEL_TOKEN)}</li>),
44
+ 'active' => %(<li class="page-item active"><a role="link" class="page-link" ) +
45
+ %(aria-current="page" aria-disabled="true">#{LABEL_TOKEN}</a></li>),
46
+ 'gap' => %(<li class="page-item gap disabled"><a role="link" class="page-link" aria-disabled="true">#{
47
+ pagy_t 'pagy.gap'}</a></li>),
48
+ 'after' => %(#{bootstrap_next_html pagy, link}</ul>) }
42
49
 
43
50
  %(<nav#{p_id} class="#{'pagy-rjs ' if sequels.size > 1}pagy-bootstrap-nav-js" #{
44
- pagy_data(pagy, :nav, tags, sequels, pagy.label_sequels(sequels))}></nav>)
51
+ nav_aria_label_attr(pagy, nav_aria_label, nav_i18n_key)} #{
52
+ pagy_data(pagy, :nav, tags, sequels, pagy.label_sequels(sequels))
53
+ }></nav>)
45
54
  end
46
55
 
47
56
  # Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the pagy.js file
48
- def pagy_bootstrap_combo_nav_js(pagy, pagy_id: nil, link_extra: '')
57
+ def pagy_bootstrap_combo_nav_js(pagy, pagy_id: nil, link_extra: '',
58
+ nav_aria_label: nil, nav_i18n_key: nil)
49
59
  p_id = %( id="#{pagy_id}") if pagy_id
50
- link = pagy_link_proc(pagy, link_extra: link_extra)
60
+ link = pagy_link_proc(pagy, link_extra:)
51
61
  p_page = pagy.page
52
62
  p_pages = pagy.pages
53
63
  input = %(<input type="number" min="1" max="#{p_pages}" value="#{
54
64
  p_page}" style="padding: 0; border: none; text-align: center; width: #{
55
- p_pages.to_s.length + 1}rem;">)
65
+ p_pages.to_s.length + 1}rem;" aria-current="page">)
56
66
 
57
- %(<nav#{p_id} class="pagy-bootstrap-combo-nav-js pagination"><div class="btn-group" role="group" #{
58
- pagy_data(pagy, :combo, pagy_marked_link(link))}>#{
67
+ %(<nav#{p_id} class="pagy-bootstrap-combo-nav-js pagination" #{
68
+ nav_aria_label_attr(pagy, nav_aria_label, nav_i18n_key)} #{
69
+ pagy_data(pagy, :combo, pagy_marked_link(link)) }><div class="btn-group" role="group">#{
59
70
  if (p_prev = pagy.prev)
60
- link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"'
71
+ link.call(p_prev, pagy_t('pagy.prev'), %(class="prev btn btn-primary" #{prev_aria_label_attr}))
61
72
  else
62
- %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>)
73
+ %(<a role="link" class="prev btn btn-primary disabled" aria-disabled="true" #{
74
+ prev_aria_label_attr}>#{pagy_t('pagy.prev')}</a>)
63
75
  end
64
76
  }<div class="pagy-combo-input btn btn-secondary" style="white-space: nowrap;">#{
65
77
  pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages}</div>#{
66
- if (p_next = pagy.next)
67
- link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"'
78
+ if (p_next = pagy.next)
79
+ link.call(p_next, pagy_t('pagy.next'), %(class="next btn btn-primary" #{next_aria_label_attr}))
68
80
  else
69
- %(<a class="next btn btn-primary disabled" href="#">#{pagy_t 'pagy.nav.next'}</a>)
81
+ %(<a role="link" class="next btn btn-primary disabled" aria-disabled="true" #{
82
+ next_aria_label_attr}>#{pagy_t 'pagy.next'}</a>)
70
83
  end
71
84
  }</div></nav>)
72
85
  end
73
86
 
74
87
  private
75
88
 
76
- def pagy_bootstrap_prev_html(pagy, link)
89
+ def bootstrap_prev_html(pagy, link)
77
90
  if (p_prev = pagy.prev)
78
- %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
91
+ %(<li class="page-item prev">#{link.call(p_prev, pagy_t('pagy.prev'), prev_aria_label_attr)}</li>)
79
92
  else
80
- %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.prev'}</a></li>)
93
+ %(<li class="page-item prev disabled"><a role="link" class="page-link" aria-disabled="true" #{
94
+ prev_aria_label_attr}>#{pagy_t('pagy.prev')}</a></li>)
81
95
  end
82
96
  end
83
97
 
84
- def pagy_bootstrap_next_html(pagy, link)
98
+ def bootstrap_next_html(pagy, link)
85
99
  if (p_next = pagy.next)
86
- %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
100
+ %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.next'), next_aria_label_attr}</li>)
87
101
  else
88
- %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.next'}</a></li>)
102
+ %(<li class="page-item next disabled"><a role="link" class="page-link" aria-disabled="true" #{
103
+ next_aria_label_attr}>#{pagy_t('pagy.next')}</a></li>)
89
104
  end
90
105
  end
91
106
  end
@@ -4,26 +4,30 @@
4
4
  require 'pagy/extras/frontend_helpers'
5
5
 
6
6
  class Pagy # :nodoc:
7
+ DEFAULT[:bulma_nav_classes] = 'is-centered'
8
+
7
9
  # Frontend modules are specially optimized for performance.
8
10
  # The resulting code may not look very elegant, but produces the best benchmarks
9
11
  module BulmaExtra
10
12
  # Pagination for bulma: it returns the html with the series of links to the pages
11
- def pagy_bulma_nav(pagy, pagy_id: nil, link_extra: '', **vars)
13
+ def pagy_bulma_nav(pagy, pagy_id: nil, link_extra: '',
14
+ nav_aria_label: nil, nav_i18n_key: nil, **vars)
12
15
  p_id = %( id="#{pagy_id}") if pagy_id
13
- link = pagy_link_proc(pagy, link_extra: link_extra)
16
+ link = pagy_link_proc(pagy, link_extra:)
14
17
 
15
- html = +%(<nav#{p_id} class="pagy-bulma-nav pagination is-centered" aria-label="pagination">)
16
- html << pagy_bulma_prev_next_html(pagy, link)
18
+ html = +%(<nav#{p_id} class="pagy-bulma-nav pagination #{DEFAULT[:bulma_nav_classes]}" #{
19
+ nav_aria_label_attr(pagy, nav_aria_label, nav_i18n_key)}>)
20
+ html << bulma_prev_next_html(pagy, link)
17
21
  html << %(<ul class="pagination-list">)
18
22
  pagy.series(**vars).each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
19
23
  html << case item
20
24
  when Integer
21
- %(<li>#{link.call item, pagy.label_for(item), %(class="pagination-link" aria-label="goto page #{item}")}</li>)
25
+ %(<li>#{link.call(item, pagy.label_for(item), %(class="pagination-link"))}</li>)
22
26
  when String
23
- %(<li>#{link.call item, pagy.label_for(item),
24
- %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>)
27
+ %(<li><a role="link" class="pagination-link is-current" aria-current="page" aria-disabled="true">#{
28
+ pagy.label_for(item)}</a></li>)
25
29
  when :gap
26
- %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap'}</span></li>)
30
+ %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.gap'}</span></li>)
27
31
  else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
28
32
  end
29
33
  end
@@ -31,62 +35,69 @@ class Pagy # :nodoc:
31
35
  end
32
36
 
33
37
  # Javascript pagination for bulma: it returns a nav and a JSON tag used by the Pagy.nav javascript
34
- def pagy_bulma_nav_js(pagy, pagy_id: nil, link_extra: '', **vars)
38
+ def pagy_bulma_nav_js(pagy, pagy_id: nil, link_extra: '',
39
+ nav_aria_label: nil, nav_i18n_key: nil, **vars)
35
40
  sequels = pagy.sequels(**vars)
36
41
  p_id = %( id="#{pagy_id}") if pagy_id
37
- link = pagy_link_proc(pagy, link_extra: link_extra)
38
- tags = { 'before' => %(#{pagy_bulma_prev_next_html(pagy, link)}<ul class="pagination-list">),
39
- 'link' => %(<li>#{link.call PAGE_PLACEHOLDER, LABEL_PLACEHOLDER,
40
- %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}")}</li>),
41
- 'active' => %(<li>#{link.call PAGE_PLACEHOLDER, LABEL_PLACEHOLDER,
42
- %(class="pagination-link is-current" aria-current="page" aria-label="page #{
43
- PAGE_PLACEHOLDER}")}</li>),
44
- 'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap'}</span></li>),
42
+ link = pagy_link_proc(pagy, link_extra:)
43
+ tags = { 'before' => %(#{bulma_prev_next_html(pagy, link)}<ul class="pagination-list">),
44
+ 'link' => %(<li>#{link.call(PAGE_TOKEN, LABEL_TOKEN, %(class="pagination-link"))}</li>),
45
+ 'active' => %(<li><a role="link" class="pagination-link is-current" aria-current="page" aria-disabled="true">#{
46
+ LABEL_TOKEN}</a></li>),
47
+ 'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.gap'}</span></li>),
45
48
  'after' => '</ul>' }
46
49
 
47
- %(<nav#{p_id} class="#{'pagy-rjs ' if sequels.size > 1}pagy-bulma-nav-js pagination is-centered" aria-label="pagination" #{
50
+ %(<nav#{p_id} class="#{'pagy-rjs ' if sequels.size > 1}pagy-bulma-nav-js pagination #{DEFAULT[:bulma_nav_classes]}" #{
51
+ nav_aria_label_attr(pagy, nav_aria_label, nav_i18n_key)}#{
48
52
  pagy_data(pagy, :nav, tags, sequels, pagy.label_sequels(sequels))}></nav>)
49
53
  end
50
54
 
51
55
  # Javascript combo pagination for bulma: it returns a nav and a JSON tag used by the pagy.js file
52
- def pagy_bulma_combo_nav_js(pagy, pagy_id: nil, link_extra: '')
56
+ def pagy_bulma_combo_nav_js(pagy, pagy_id: nil, link_extra: '',
57
+ nav_aria_label: nil, nav_i18n_key: nil)
53
58
  p_id = %( id="#{pagy_id}") if pagy_id
54
- link = pagy_link_proc(pagy, link_extra: link_extra)
59
+ link = pagy_link_proc(pagy, link_extra:)
55
60
  p_page = pagy.page
56
61
  p_pages = pagy.pages
57
62
  input = %(<input class="input" type="number" min="1" max="#{p_pages}" value="#{
58
- p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length + 1}rem; margin:0 0.3rem;">)
63
+ p_page}" style="padding: 0; text-align: center; width: #{
64
+ p_pages.to_s.length + 1}rem; margin:0 0.3rem;" aria-current="page">)
59
65
 
60
- html = %(<nav#{p_id} class="pagy-bulma-combo-nav-js" aria-label="pagination">)
61
- %(#{html}<div class="field is-grouped is-grouped-centered" role="group" #{
62
- pagy_data(pagy, :combo, pagy_marked_link(link))}>#{
66
+ html = %(<nav#{p_id} class="pagy-bulma-combo-nav-js #{DEFAULT[:bulma_nav_classes]}" #{
67
+ nav_aria_label_attr(pagy, nav_aria_label, nav_i18n_key)} #{
68
+ pagy_data(pagy, :combo, pagy_marked_link(link))}>)
69
+ %(#{html}<div class="field is-grouped is-grouped-centered" role="group">#{
63
70
  if (p_prev = pagy.prev)
64
- %(<p class="control">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"'}</p>)
71
+ %(<p class="control">#{link.call(p_prev, pagy_t('pagy.prev'), %(class="button" #{prev_aria_label_attr}))}</p>)
65
72
  else
66
- %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.prev'}</a></p>)
73
+ %(<p class="control"><a role="link" class="button" disabled aria-disabled="true" #{
74
+ prev_aria_label_attr}>#{pagy_t 'pagy.prev'}</a></p>)
67
75
  end
68
76
  }<div class="pagy-combo-input control level is-mobile">#{
69
77
  pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages}</div>#{
70
78
  if (p_next = pagy.next)
71
- %(<p class="control">#{link.call p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"'}</p>)
79
+ %(<p class="control">#{link.call(p_next, pagy_t('pagy.next'), %(class="button" #{next_aria_label_attr}))}</p>)
72
80
  else
73
- %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.next'}</a></p>)
81
+ %(<p class="control"><a role="link" class="button" disabled aria-disabled="true"#{
82
+ next_aria_label_attr}>#{pagy_t 'pagy.next'}</a></p>)
74
83
  end
75
84
  }</div></nav>)
76
85
  end
77
86
 
78
87
  private
79
88
 
80
- def pagy_bulma_prev_next_html(pagy, link)
89
+ def bulma_prev_next_html(pagy, link)
81
90
  html = +if (p_prev = pagy.prev)
82
- link.call p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"'
91
+ link.call(p_prev, pagy_t('pagy.prev'), %(#{prev_aria_label_attr} class="pagination-previous"))
83
92
  else
84
- %(<a class="pagination-previous" disabled>#{pagy_t 'pagy.nav.prev'}</a>)
93
+ %(<a role="link" class="pagination-previous" disabled aria-disabled="true" #{
94
+ prev_aria_label_attr}>#{pagy_t 'pagy.prev'}</a>)
85
95
  end
86
96
  html << if (p_next = pagy.next)
87
- link.call p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"'
97
+ link.call(p_next, pagy_t('pagy.next'), %(#{next_aria_label_attr} class="pagination-next"))
88
98
  else
89
- %(<a class="pagination-next" disabled>#{pagy_t 'pagy.nav.next'}</a>)
99
+ %(<a role="link" class="pagination-next" disabled aria-disabled="true" #{
100
+ next_aria_label_attr}>#{pagy_t('pagy.next')}</a>)
90
101
  end
91
102
  end
92
103
  end
@@ -8,18 +8,17 @@ class Pagy # :nodoc:
8
8
  # Add pagination filtering by calendar unit (:year, :quarter, :month, :week, :day) to the regular pagination
9
9
  module CalendarExtra
10
10
  # Additions for the Backend module
11
- module Backend
11
+ module BackendAddOn
12
12
  CONF_KEYS = (Calendar::UNITS + %i[pagy active]).freeze
13
13
 
14
14
  private
15
15
 
16
16
  # Take a collection and a conf Hash with keys in CONF_KEYS and return an array with 3 items: [calendar, pagy, results]
17
17
  def pagy_calendar(collection, conf)
18
- unless conf.is_a?(Hash) && (conf.keys - CONF_KEYS).empty? && conf.all? { |k, v| v.is_a?(Hash) || k == :active }
19
- raise ArgumentError, "keys must be in #{CONF_KEYS.inspect} and object values must be Hashes; got #{conf.inspect}"
20
- end
18
+ raise ArgumentError, "keys must be in #{CONF_KEYS.inspect}" \
19
+ unless conf.is_a?(Hash) && (conf.keys - CONF_KEYS).empty?
21
20
 
22
- conf[:pagy] = {} unless conf[:pagy] # use default Pagy object when omitted
21
+ conf[:pagy] ||= {}
23
22
  unless conf.key?(:active) && !conf[:active]
24
23
  calendar, from, to = Calendar::Helper.send(:init, conf, pagy_calendar_period(collection), params)
25
24
  collection = pagy_calendar_filter(collection, from, to)
@@ -42,13 +41,13 @@ class Pagy # :nodoc:
42
41
  end
43
42
 
44
43
  # Additions for the Frontend module
45
- module UrlHelper
44
+ module UrlHelperAddOn
46
45
  # Return the url for the calendar page at time
47
46
  def pagy_calendar_url_at(calendar, time, **opts)
48
47
  pagy_url_for(calendar.send(:calendar_at, time, **opts), 1, **opts)
49
48
  end
50
49
  end
51
50
  end
52
- Backend.prepend CalendarExtra::Backend, CalendarExtra::UrlHelper
53
- Frontend.prepend CalendarExtra::UrlHelper
51
+ Backend.prepend CalendarExtra::BackendAddOn, CalendarExtra::UrlHelperAddOn
52
+ Frontend.prepend CalendarExtra::UrlHelperAddOn
54
53
  end
@@ -19,7 +19,7 @@ class Pagy # :nodoc:
19
19
  total.is_a?(Hash) ? total['value'] : total
20
20
  end
21
21
 
22
- module ElasticsearchRails # :nodoc:
22
+ module ModelExtension # :nodoc:
23
23
  # Return an array used to delay the call of #search
24
24
  # after the pagination variables are merged to the options.
25
25
  # It also pushes to the same array an optional method call.
@@ -30,9 +30,10 @@ class Pagy # :nodoc:
30
30
  end
31
31
  alias_method Pagy::DEFAULT[:elasticsearch_rails_pagy_search], :pagy_elasticsearch_rails
32
32
  end
33
+ Pagy::ElasticsearchRails = ModelExtension
33
34
 
34
35
  # Additions for the Pagy class
35
- module Pagy
36
+ module PagyAddOn
36
37
  # Create a Pagy object from an Elasticsearch::Model::Response::Response object
37
38
  def new_from_elasticsearch_rails(response, vars = {})
38
39
  vars[:items] = response.search.options[:size] || 10
@@ -41,9 +42,10 @@ class Pagy # :nodoc:
41
42
  new(vars)
42
43
  end
43
44
  end
45
+ Pagy.extend PagyAddOn
44
46
 
45
47
  # Add specialized backend methods to paginate ElasticsearchRails searches
46
- module Backend
48
+ module BackendAddOn
47
49
  private
48
50
 
49
51
  # Return Pagy object and items
@@ -73,8 +75,6 @@ class Pagy # :nodoc:
73
75
  vars
74
76
  end
75
77
  end
78
+ Backend.prepend BackendAddOn
76
79
  end
77
- ElasticsearchRails = ElasticsearchRailsExtra::ElasticsearchRails
78
- extend ElasticsearchRailsExtra::Pagy
79
- Backend.prepend ElasticsearchRailsExtra::Backend
80
80
  end