pagy 3.13.0 → 4.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/lib/config/pagy.rb +36 -25
  4. data/lib/javascripts/pagy.js +104 -93
  5. data/lib/locales/ar.yml +26 -0
  6. data/lib/locales/bg.yml +2 -2
  7. data/lib/locales/bs.yml +2 -2
  8. data/lib/locales/ca.yml +2 -2
  9. data/lib/locales/cs.yml +2 -2
  10. data/lib/locales/da.yml +2 -2
  11. data/lib/locales/de.yml +2 -2
  12. data/lib/locales/en.yml +2 -2
  13. data/lib/locales/es.yml +2 -2
  14. data/lib/locales/fr.yml +2 -2
  15. data/lib/locales/hr.yml +2 -2
  16. data/lib/locales/id.yml +2 -2
  17. data/lib/locales/it.yml +2 -2
  18. data/lib/locales/ja.yml +2 -2
  19. data/lib/locales/km.yml +2 -2
  20. data/lib/locales/ko.yml +2 -2
  21. data/lib/locales/nb.yml +2 -2
  22. data/lib/locales/nl.yml +2 -2
  23. data/lib/locales/pl.yml +2 -2
  24. data/lib/locales/pt-BR.yml +2 -2
  25. data/lib/locales/pt.yml +2 -2
  26. data/lib/locales/ru.yml +2 -2
  27. data/lib/locales/sr.yml +2 -2
  28. data/lib/locales/sv-SE.yml +2 -2
  29. data/lib/locales/sv.yml +2 -2
  30. data/lib/locales/sw.yml +2 -2
  31. data/lib/locales/tr.yml +2 -2
  32. data/lib/locales/uk.yml +24 -0
  33. data/lib/locales/utils/i18n.rb +3 -11
  34. data/lib/locales/utils/loader.rb +7 -10
  35. data/lib/locales/utils/p11n.rb +44 -23
  36. data/lib/locales/zh-CN.yml +2 -2
  37. data/lib/locales/zh-HK.yml +2 -2
  38. data/lib/locales/zh-TW.yml +3 -3
  39. data/lib/pagy.rb +51 -40
  40. data/lib/pagy/backend.rb +5 -3
  41. data/lib/pagy/console.rb +21 -0
  42. data/lib/pagy/countless.rb +13 -10
  43. data/lib/pagy/deprecation.rb +27 -0
  44. data/lib/pagy/exceptions.rb +6 -2
  45. data/lib/pagy/extras/arel.rb +4 -3
  46. data/lib/pagy/extras/array.rb +4 -3
  47. data/lib/pagy/extras/bootstrap.rb +61 -30
  48. data/lib/pagy/extras/bulma.rb +60 -38
  49. data/lib/pagy/extras/countless.rb +7 -8
  50. data/lib/pagy/extras/elasticsearch_rails.rb +28 -17
  51. data/lib/pagy/extras/foundation.rb +61 -32
  52. data/lib/pagy/extras/headers.rb +13 -9
  53. data/lib/pagy/extras/i18n.rb +10 -11
  54. data/lib/pagy/extras/items.rb +21 -34
  55. data/lib/pagy/extras/materialize.rb +58 -35
  56. data/lib/pagy/extras/meilisearch.rb +55 -0
  57. data/lib/pagy/extras/metadata.rb +27 -21
  58. data/lib/pagy/extras/navs.rb +44 -20
  59. data/lib/pagy/extras/overflow.rb +52 -48
  60. data/lib/pagy/extras/searchkick.rb +27 -16
  61. data/lib/pagy/extras/semantic.rb +55 -30
  62. data/lib/pagy/extras/shared.rb +14 -15
  63. data/lib/pagy/extras/standalone.rb +62 -0
  64. data/lib/pagy/extras/support.rb +20 -11
  65. data/lib/pagy/extras/trim.rb +12 -11
  66. data/lib/pagy/extras/uikit.rb +54 -35
  67. data/lib/pagy/frontend.rb +64 -33
  68. metadata +10 -8
  69. data/lib/pagy/extras/pagy_search.rb +0 -18
data/lib/locales/hr.yml CHANGED
@@ -19,6 +19,6 @@ hr:
19
19
  single_page: "Prikazuje se <b>%{count}</b> %{item_name}"
20
20
  multiple_pages: "Prikaz %{item_name} <b>%{from}-%{to}</b> od <b>%{count}</b> ukupno"
21
21
 
22
- combo_nav_js: "Stranica %{page_input} od %{pages}"
22
+ combo_nav_js: "<label>Stranica %{page_input} od %{pages}</label>"
23
23
 
24
- items_selector_js: "Prikaži %{items_input} %{item_name} po stranici"
24
+ items_selector_js: "<label>Prikaži %{items_input} %{item_name} po stranici</label>"
data/lib/locales/id.yml CHANGED
@@ -15,6 +15,6 @@ id:
15
15
  single_page: "Menampilkan <b>%{count}</b> %{item_name}"
16
16
  multiple_pages: "Menampilkan %{item_name} <b>%{from}-%{to}</b> dari total <b>%{count}</b>"
17
17
 
18
- combo_nav_js: "Halaman %{page_input} dari %{pages}"
18
+ combo_nav_js: "<label>Halaman %{page_input} dari %{pages}</label>"
19
19
 
20
- items_selector_js: "Tampilkan %{items_input} %{item_name} per halaman"
20
+ items_selector_js: "<label>Tampilkan %{items_input} %{item_name} per halaman</label>"
data/lib/locales/it.yml CHANGED
@@ -17,6 +17,6 @@ it:
17
17
  single_page: "Visualizzazione di <b>%{count}</b> %{item_name}"
18
18
  multiple_pages: "Visualizzazione %{item_name} <b>%{from}-%{to}</b> di <b>%{count}</b> in totale"
19
19
 
20
- combo_nav_js: "Pagina %{page_input} di %{pages}"
20
+ combo_nav_js: "<label>Pagina %{page_input} di %{pages}</label>"
21
21
 
22
- items_selector_js: "Mostra %{items_input} %{item_name} per pagina"
22
+ items_selector_js: "<label>Mostra %{items_input} %{item_name} per pagina</label>"
data/lib/locales/ja.yml CHANGED
@@ -15,6 +15,6 @@ ja:
15
15
  single_page: "<b>%{count}</b> 件の%{item_name}"
16
16
  multiple_pages: "<b>%{count}</b> 件中 <b>%{from}-%{to}</b> 件目の%{item_name}を表示中"
17
17
 
18
- combo_nav_js: "%{page_input} / %{pages} ページ"
18
+ combo_nav_js: "<label>%{page_input} / %{pages} ページ</label>"
19
19
 
20
- items_selector_js: "%{items_input} 件ずつ表示"
20
+ items_selector_js: "<label>%{items_input} 件ずつ表示</label>"
data/lib/locales/km.yml CHANGED
@@ -14,6 +14,6 @@ km:
14
14
  single_page: "បង្ហាញ <b>%{count}</b> %{item_name}"
15
15
  multiple_pages: "បង្ហាញ %{item_name} <b>%{from}-%{to}</b> នៃ <b>%{count}</b> ជាចំនួនសរុប"
16
16
 
17
- combo_nav_js: "ទំព័រ %{page_input} នៃ %{pages}"
17
+ combo_nav_js: "<label>ទំព័រ %{page_input} នៃ %{pages}</label>"
18
18
 
19
- items_selector_js: "បង្ហាញ %{items_input} %{item_name} ក្នុង ១ ទំព័រ"
19
+ items_selector_js: "<label>បង្ហាញ %{items_input} %{item_name} ក្នុង ១ ទំព័រ</label>"
data/lib/locales/ko.yml CHANGED
@@ -15,6 +15,6 @@ ko:
15
15
  single_page: "<b>%{count}</b>개의 %{item_name} 표시됨"
16
16
  multiple_pages: "총 <b>%{count}</b>개의 %{item_name} 중 <b>%{from}-%{to}</b>"
17
17
 
18
- combo_nav_js: " %{pages} 중 %{page_input} 페이지"
18
+ combo_nav_js: "<label>총 %{pages} 중 %{page_input} 페이지</label>"
19
19
 
20
- items_selector_js: "페이지 당 %{items_input}개 표시%"
20
+ items_selector_js: "<label>페이지 당 %{items_input}개 표시%</label>"
data/lib/locales/nb.yml CHANGED
@@ -17,6 +17,6 @@ nb:
17
17
  single_page: "Viser <b>%{count}</b> %{item_name}"
18
18
  multiple_pages: "Viser %{item_name} <b>%{from}-%{to}</b> av totalt <b>%{count}</b>"
19
19
 
20
- combo_nav_js: "Side %{page_input} av %{pages}"
20
+ combo_nav_js: "<label>Side %{page_input} av %{pages}</label>"
21
21
 
22
- items_selector_js: "Vis %{items_input} %{item_name} per side"
22
+ items_selector_js: "<label>Vis %{items_input} %{item_name} per side</label>"
data/lib/locales/nl.yml CHANGED
@@ -17,6 +17,6 @@ nl:
17
17
  single_page: "Toont <b>%{count}</b> %{item_name}"
18
18
  multiple_pages: "Toont <b>%{from}-%{to}</b> %{item_name} van <b>%{count}</b> in totaal"
19
19
 
20
- combo_nav_js: "Pagina %{page_input} van %{pages}"
20
+ combo_nav_js: "<label>Pagina %{page_input} van %{pages}</label>"
21
21
 
22
- items_selector_js: "Toont %{items_input} %{item_name} per pagina"
22
+ items_selector_js: "<label>Toont %{items_input} %{item_name} per pagina</label>"
data/lib/locales/pl.yml CHANGED
@@ -19,6 +19,6 @@ pl:
19
19
  single_page: "Wyświetlono <b>%{count}</b> %{item_name}"
20
20
  multiple_pages: "Wyświetlono %{item_name} <b>%{from}-%{to}</b> z wszystkich <b>%{count}</b>"
21
21
 
22
- combo_nav_js: "Strona %{page_input} z %{pages}"
22
+ combo_nav_js: "<label>Strona %{page_input} z %{pages}</label>"
23
23
 
24
- items_selector_js: "Pokaż %{items_input} %{item_name} na stronie"
24
+ items_selector_js: "<label>Pokaż %{items_input} %{item_name} na stronie</label>"
@@ -17,6 +17,6 @@ pt-BR:
17
17
  single_page: "Mostrando %{count} %{item_name}"
18
18
  multiple_pages: "Mostrando %{item_name} %{from}-%{to} do total de %{count}"
19
19
 
20
- combo_nav_js: "Página %{page_input} de %{pages}"
20
+ combo_nav_js: "<label>Página %{page_input} de %{pages}</label>"
21
21
 
22
- items_selector_js: "Mostrar %{items_input} %{item_name} por página"
22
+ items_selector_js: "<label>Mostrar %{items_input} %{item_name} por página</label>"
data/lib/locales/pt.yml CHANGED
@@ -17,6 +17,6 @@ pt:
17
17
  single_page: "Mostrando %{count} %{item_name}"
18
18
  multiple_pages: "Mostrando %{item_name} %{from}-%{to} de um total de %{count}"
19
19
 
20
- combo_nav_js: "Página %{page_input} de %{pages}"
20
+ combo_nav_js: "<label>Página %{page_input} de %{pages}</label>"
21
21
 
22
- items_selector_js: "Mostrar %{items_input} %{item_name} por página"
22
+ items_selector_js: "<label>Mostrar %{items_input} %{item_name} por página</label>"
data/lib/locales/ru.yml CHANGED
@@ -19,6 +19,6 @@ ru:
19
19
  single_page: "<b>%{count}</b> %{item_name}"
20
20
  multiple_pages: "Всего <b>%{count}</b> %{item_name}, показаны с <b>%{from}</b> по <b>%{to}</b>"
21
21
 
22
- combo_nav_js: "Страница %{page_input} из %{pages}"
22
+ combo_nav_js: "<label>Страница %{page_input} из %{pages}</label>"
23
23
 
24
- items_selector_js: "Показать %{items_input} %{item_name} на странице"
24
+ items_selector_js: "<label>Показать %{items_input} %{item_name} на странице</label>"
data/lib/locales/sr.yml CHANGED
@@ -18,6 +18,6 @@ sr:
18
18
  single_page: "Приказује се <b>%{count}</b> %{item_name}"
19
19
  multiple_pages: "Приказ %{item_name} <b>%{from}-%{to}</b> од <b>%{count}</b> укупно"
20
20
 
21
- combo_nav_js: "Страниca %{page_input} од %{pages}"
21
+ combo_nav_js: "<label>Страниca %{page_input} од %{pages}</label>"
22
22
 
23
- items_selector_js: "Прикажи %{items_input} %{item_name} по страниcи"
23
+ items_selector_js: "<label>Прикажи %{items_input} %{item_name} по страниcи</label>"
@@ -18,6 +18,6 @@ sv-SE:
18
18
  single_page: "Visar <b>%{count}</b> %{item_name}"
19
19
  multiple_pages: "Visar %{item_name} <b>%{from}-%{to}</b> av <b>%{count}</b> totalt"
20
20
 
21
- combo_nav_js: "Sida %{page_input} av %{pages}"
21
+ combo_nav_js: "<label>Sida %{page_input} av %{pages}</label>"
22
22
 
23
- items_selector_js: "Visa %{items_input} %{item_name} per sida"
23
+ items_selector_js: "<label>Visa %{items_input} %{item_name} per sida</label>"
data/lib/locales/sv.yml CHANGED
@@ -18,6 +18,6 @@ sv:
18
18
  single_page: "Visar <b>%{count}</b> %{item_name}"
19
19
  multiple_pages: "Visar %{item_name} <b>%{from}-%{to}</b> av <b>%{count}</b> totalt"
20
20
 
21
- combo_nav_js: "Sida %{page_input} av %{pages}"
21
+ combo_nav_js: "<label>Sida %{page_input} av %{pages}</label>"
22
22
 
23
- items_selector_js: "Visa %{items_input} %{item_name} per sida"
23
+ items_selector_js: "<label>Visa %{items_input} %{item_name} per sida</label>"
data/lib/locales/sw.yml CHANGED
@@ -17,6 +17,6 @@ sw:
17
17
  single_page: "Inaonyesha %{item_name} <b>%{count}</b>"
18
18
  multiple_pages: "Inaonyesha %{item_name} <b>%{from}-%{to}</b> ya <b>%{count}</b> kwa jumla"
19
19
 
20
- combo_nav_js: "Kurasa %{page_input} ya %{pages}"
20
+ combo_nav_js: "<label>Kurasa %{page_input} ya %{pages}</label>"
21
21
 
22
- items_selector_js: "Onyesha %{items_input} %{item_name} kwa kila ukurasa"
22
+ items_selector_js: "<label>Onyesha %{items_input} %{item_name} kwa kila ukurasa</label>"
data/lib/locales/tr.yml CHANGED
@@ -15,6 +15,6 @@ tr:
15
15
  single_page: "Toplam <b>%{count}</b> %{item_name} gösteriliyor"
16
16
  multiple_pages: "<b>%{count}</b> %{item_name} içerisinden <b>%{from}-%{to}</b> kadarı gösteriliyor"
17
17
 
18
- combo_nav_js: "Sayfa %{page_input} / %{pages}"
18
+ combo_nav_js: "<label>Sayfa %{page_input} / %{pages}</label>"
19
19
 
20
- items_selector_js: "Sayfada %{items_input} %{item_name} göster"
20
+ items_selector_js: "<label>Sayfada %{items_input} %{item_name} göster</label>"
@@ -0,0 +1,24 @@
1
+ # :east_slavic pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
2
+
3
+ uk:
4
+ pagy:
5
+
6
+ item_name:
7
+ one: "запис"
8
+ few: "записів"
9
+ many: "записів"
10
+ other: "записів"
11
+
12
+ nav:
13
+ prev: "&lsaquo;&nbsp;Назад"
14
+ next: "Далі&nbsp;&rsaquo;"
15
+ gap: "&hellip;"
16
+
17
+ info:
18
+ no_items: "Більше %{item_name} немає"
19
+ single_page: "<b>%{count}</b> %{item_name}"
20
+ multiple_pages: "Загалом <b>%{count}</b> %{item_name}, показані з <b>%{from}</b> по <b>%{to}</b>"
21
+
22
+ combo_nav_js: "<label>Сторінка %{page_input} з %{pages}</label>"
23
+
24
+ items_selector_js: "<label>Показати %{items_input} %{item_name} на сторінці</label>"
@@ -1,25 +1,17 @@
1
1
  # See https://ddnexus.github.io/pagy/api/frontend#i18n
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  # this file returns the I18n hash used as default alternative to the i18n gem
6
5
 
7
- Pagy::DEPRECATED_LOCALES = {'pt-br' =>'pt-BR', 'se' => 'sv-SE'}
8
-
9
6
  Hash.new{|h,_| h.first[1]}.tap do |i18n_hash| # first loaded locale used as default
10
7
  i18n_hash.define_singleton_method(:load) do |*load_args|
11
8
  # eval: we don't need to keep the loader proc in memory
12
9
  eval(Pagy.root.join('locales', 'utils', 'loader.rb').read).call(i18n_hash, *load_args) #rubocop:disable Security/Eval
13
10
  end
14
- i18n_hash.define_singleton_method(:t) do |locale, path, vars={}|
15
- if Pagy::DEPRECATED_LOCALES.key?(locale)
16
- new_locale = Pagy::DEPRECATED_LOCALES[locale]
17
- $stderr.puts("WARNING: the Pagy locale '#{locale}' is deprecated; use '#{new_locale}' instead")
18
- locale = new_locale
19
- end
11
+ i18n_hash.define_singleton_method(:t) do |locale, key, **opts|
20
12
  data, pluralize = self[locale]
21
- translate = data[path] || vars[:count] && data[path+=".#{pluralize.call(vars[:count])}"] or return %([translation missing: "#{path}"])
22
- translate.call(vars)
13
+ translate = data[key] || opts[:count] && data[key+=".#{pluralize.call(opts[:count])}"] or return %([translation missing: "#{key}"])
14
+ translate.call(opts)
23
15
  end
24
16
  i18n_hash.load(locale: 'en')
25
17
  end
@@ -1,18 +1,20 @@
1
- # encoding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
3
  # the whole file will be eval'ed/executed and gc-collected after returning/executing the loader proc
5
4
 
6
5
  # eval: no need for the whole file in memory
7
- plurals, _ = eval(Pagy.root.join('locales', 'utils', 'p11n.rb').read) #rubocop:disable Security/Eval
6
+ plurals, = eval Pagy.root.join('locales', 'utils', 'p11n.rb').read # rubocop:disable Security/Eval
8
7
 
9
8
  # flatten the dictionary file nested keys
10
9
  # convert each value to a simple ruby interpolation proc
11
10
  flatten = lambda do |hash, key=''|
12
11
  hash.each.reduce({}) do |h, (k, v)|
13
- v.is_a?(Hash) \
14
- ? h.merge!(flatten.call(v, "#{key}#{k}."))
15
- : h.merge!(eval %({"#{key}#{k}" => lambda{|vars|"#{v.gsub(/%{[^}]+?}/){|m| "\#{vars[:#{m[2..-2]}]||'#{m}'}" }}"}})) #rubocop:disable Security/Eval
12
+ if v.is_a?(Hash)
13
+ h.merge! flatten.call(v, "#{key}#{k}.")
14
+ else
15
+ code = %({"#{key}#{k}" => lambda{|vars|"#{v.gsub(/%{[^}]+?}/){|m| "\#{vars[:#{m[2..-2]}]||'#{m}'}" }}"}})
16
+ h.merge! eval(code) # rubocop:disable Security/Eval
17
+ end
16
18
  end
17
19
  end
18
20
 
@@ -20,11 +22,6 @@ flatten = lambda do |hash, key=''|
20
22
  lambda do |i18n, *args|
21
23
  i18n.clear
22
24
  args.each do |arg|
23
- if Pagy::DEPRECATED_LOCALES.key?(arg[:locale])
24
- new_locale = Pagy::DEPRECATED_LOCALES[arg[:locale]]
25
- $stderr.puts("WARNING: the Pagy locale '#{arg[:locale]}' is deprecated; use '#{new_locale}' instead")
26
- arg[:locale] = new_locale
27
- end
28
25
  arg[:filepath] ||= Pagy.root.join('locales', "#{arg[:locale]}.yml")
29
26
  arg[:pluralize] ||= plurals[arg[:locale]]
30
27
  hash = YAML.load(File.read(arg[:filepath], encoding: 'UTF-8')) #rubocop:disable Security/YAMLLoad
@@ -1,5 +1,4 @@
1
1
  # See https://ddnexus.github.io/pagy/api/frontend#i18n
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  # This file adds support for multiple built-in plualization types.
@@ -18,48 +17,68 @@ from12to14 = [12,13,14].freeze
18
17
  # Each proc may apply to one or more locales below.
19
18
  # Pluralization logic adapted from https://github.com/svenfuchs/rails-i18n
20
19
  p11n = {
21
- one_other: lambda {|n| n == 1 ? 'one' : 'other'}, # default
20
+ one_other: -> (n){ n == 1 ? 'one' : 'other' }, # default
22
21
 
23
- east_slavic: lambda do |n|
22
+ arabic: lambda do |n|
24
23
  n ||= 0
25
- mod10 = n % 10
26
24
  mod100 = n % 100
27
25
 
28
- if mod10 == 1 && mod100 != 11 ; 'one'
29
- elsif from2to4.include?(mod10) && !from12to14.include?(mod100) ; 'few'
30
- elsif mod10 == 0 || from5to9.include?(mod10) || from11to14.include?(mod100) ; 'many'
31
- else 'other'
26
+ case
27
+ when n == 0 then 'zero' # rubocop:disable Style/NumericPredicate
28
+ when n == 1 then 'one'
29
+ when n == 2 then 'two'
30
+ when (3..10).to_a.include?(mod100) then 'few'
31
+ when (11..99).to_a.include?(mod100) then 'many'
32
+ else 'other'
32
33
  end
33
34
  end,
34
35
 
35
- west_slavic: lambda do |n|
36
- if n == 1 ; 'one'
37
- elsif [2, 3, 4].include?(n) ; 'few'
38
- else ; 'other'
39
- end
36
+ east_slavic: lambda do |n|
37
+ n ||= 0
38
+ mod10 = n % 10
39
+ mod100 = n % 100
40
+
41
+ case
42
+ when mod10 == 1 && mod100 != 11 then 'one'
43
+ when from2to4.include?(mod10) && !from12to14.include?(mod100) then 'few'
44
+ when mod10 == 0 || from5to9.include?(mod10) || from11to14.include?(mod100) then 'many' # rubocop:disable Style/NumericPredicate
45
+ else 'other'
46
+ end
40
47
  end,
41
48
 
42
49
  one_two_other: lambda do |n|
43
- if n == 1 ; 'one'
44
- elsif n == 2 ; 'two'
45
- else 'other'
50
+ case n
51
+ when 1 then 'one'
52
+ when 2 then 'two'
53
+ else 'other'
46
54
  end
47
55
  end,
48
56
 
49
- one_upto_two_other: lambda {|n| n && n >= 0 && n < 2 ? 'one' : 'other'},
57
+ one_upto_two_other: -> (n){ n && n >= 0 && n < 2 ? 'one' : 'other' },
50
58
 
51
- other: Proc.new { 'other' },
59
+ other: -> (*){ 'other' },
52
60
 
53
61
  polish: lambda do |n|
54
62
  n ||= 0
55
- mod10 = n % 10
63
+ mod10 = n % 10
56
64
  mod100 = n % 100
57
- if n == 1 ; 'one'
58
- elsif from2to4.include?(mod10) && !from12to14.include?(mod100) ; 'few'
59
- elsif (from0to1 + from5to9).include?(mod10) || from12to14.include?(mod100) ; 'many'
60
- else 'other'
65
+
66
+ case
67
+ when n == 1 then 'one'
68
+ when from2to4.include?(mod10) && !from12to14.include?(mod100) then 'few'
69
+ when (from0to1 + from5to9).include?(mod10) || from12to14.include?(mod100) then 'many'
70
+ else 'other'
71
+ end
72
+ end,
73
+
74
+ west_slavic: lambda do |n|
75
+ case n
76
+ when 1 then 'one'
77
+ when 2, 3, 4 then 'few'
78
+ else 'other'
61
79
  end
62
80
  end
81
+
63
82
  }
64
83
 
65
84
  # Hash of locale/pluralization pairs
@@ -67,6 +86,7 @@ p11n = {
67
86
  # The default pluralization for locales not explicitly listed here
68
87
  # is the :one_other pluralization proc (used for English)
69
88
  plurals = Hash.new(p11n[:one_other]).tap do |hash|
89
+ hash['ar'] = p11n[:arabic]
70
90
  hash['bs'] = p11n[:east_slavic]
71
91
  hash['cs'] = p11n[:west_slavic]
72
92
  hash['id'] = p11n[:other]
@@ -81,6 +101,7 @@ plurals = Hash.new(p11n[:one_other]).tap do |hash|
81
101
  hash['sv'] = p11n[:one_two_other]
82
102
  hash['sv-SE'] = p11n[:one_two_other]
83
103
  hash['tr'] = p11n[:other]
104
+ hash['uk'] = p11n[:east_slavic]
84
105
  hash['zh-CN'] = p11n[:other]
85
106
  hash['zh-HK'] = p11n[:other]
86
107
  hash['zh-TW'] = p11n[:other]
@@ -15,6 +15,6 @@ zh-CN:
15
15
  single_page: "显示<b>%{count}</b> 项%{item_name}"
16
16
  multiple_pages: "共 <b>%{count}</b> 项%{item_name},显示 <b>%{from}-%{to}</b>"
17
17
 
18
- combo_nav_js: " %{page_input} / %{pages} "
18
+ combo_nav_js: "<label>第 %{page_input} / %{pages} 页</label>"
19
19
 
20
- items_selector_js: "每页显示 %{items_input} 条%"
20
+ items_selector_js: "<label>每页显示 %{items_input} 条%</label>"
@@ -15,6 +15,6 @@ zh-HK:
15
15
  single_page: "顯示 <b>%{count}</b> %{item_name}"
16
16
  multiple_pages: "共 <b>%{count}</b> %{item_name},顯示 <b>%{from}-%{to}</b>"
17
17
 
18
- combo_nav_js: " %{page_input} / %{pages} "
18
+ combo_nav_js: "<label>第 %{page_input} / %{pages} 頁</label>"
19
19
 
20
- items_selector_js: "每頁顯示 %{items_input} 記錄"
20
+ items_selector_js: "<label>每頁顯示 %{items_input} 記錄</label>"
@@ -10,11 +10,11 @@ zh-TW:
10
10
  next: "下一頁&nbsp;&rsaquo;"
11
11
  gap: "&hellip;"
12
12
 
13
- combo_nav_js: "第 %{page_input} / %{pages} 頁"
14
-
15
13
  info:
16
14
  no_items: "沒找到項"
17
15
  single_page: "顯示 <b>%{count}</b> 項%{item_name}"
18
16
  multiple_pages: "共 <b>%{count}</b> 項%{item_name},顯示 <b>%{from}-%{to}</b>"
19
17
 
20
- items_selector_js: "每頁顯示 %{items_input} 項%{item_name}"
18
+ combo_nav_js: "<label>第 %{page_input} / %{pages} 頁</label>"
19
+
20
+ items_selector_js: "<label>每頁顯示 %{items_input} 項%{item_name}</label>"
data/lib/pagy.rb CHANGED
@@ -1,65 +1,76 @@
1
1
  # See Pagy API documentation: https://ddnexus.github.io/pagy/api/pagy
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pathname'
6
5
 
7
- class Pagy ; VERSION = '3.13.0'
6
+ # main class
7
+ class Pagy
8
+ VERSION = '4.10.1'
8
9
 
9
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
10
- def self.root; @root ||= Pathname.new(__FILE__).dirname.freeze end
11
+ def self.root
12
+ @root ||= Pathname.new(__dir__).freeze
13
+ end
11
14
 
12
15
  # default vars
13
- VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params:{}, anchor:'', link_extra:'', i18n_key:'pagy.item_name', cycle:false }
16
+ VARS = { page: 1, items: 20, outset: 0, size: [1, 4, 4, 1], page_param: :page, # rubocop:disable Style/MutableConstant
17
+ params: {}, fragment: '', link_extra: '', i18n_key: 'pagy.item_name', cycle: false }
14
18
 
15
19
  attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next
16
20
 
21
+ INSTANCE_VARS_MIN = { count: 0, items: 1, page: 1, outset: 0 }.freeze
22
+
17
23
  # Merge and validate the options, do some simple arithmetic and set the instance variables
18
24
  def initialize(vars)
19
- @vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars
20
- { count:0, items:1, outset:0, page:1 }.each do |k,min| # validate instance variables
21
- (@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \
22
- or raise(VariableError.new(self), "expected :#{k} >= #{min}; got #{@vars[k].inspect}")
25
+ @vars = VARS.merge( vars.delete_if{|k,v| VARS.key?(k) && (v.nil? || v == '') } )
26
+ @vars[:fragment] = Pagy.deprecated_var(:anchor, @vars[:anchor], :fragment, @vars[:fragment]) if @vars[:anchor]
27
+
28
+ INSTANCE_VARS_MIN.each do |name,min|
29
+ raise VariableError.new(self), "expected :#{name} >= #{min}; got #{@vars[name].inspect}" \
30
+ unless @vars[name] && instance_variable_set(:"@#{name}", @vars[name].to_i) >= min
23
31
  end
24
- @pages = @last = [(@count.to_f / @items).ceil, 1].max # cardinal and ordinal meanings
25
- @page <= @last or raise(OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}")
26
- @offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
27
- @items = @count - ((@pages-1) * @items) if @page == @last && @count > 0 # adjust items for last non-empty page
28
- @from = @count == 0 ? 0 : @offset+1 - @outset # page begins from item
29
- @to = @count == 0 ? 0 : @offset + @items - @outset # page ends to item
30
- @prev = (@page-1 unless @page == 1) # nil if no prev page
31
- @next = @page == @last ? (1 if @vars[:cycle]) : @page+1 # nil if no next page, 1 if :cycle
32
+ @pages = @last = [(@count.to_f / @items).ceil, 1].max
33
+ raise OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}" \
34
+ if @page > @last
35
+
36
+ @offset = @items * (@page - 1) + @outset
37
+ @items = @count - ((@pages - 1) * @items) if @page == @last && @count.positive?
38
+ @from = @count.zero? ? 0 : @offset + 1 - @outset
39
+ @to = @count.zero? ? 0 : @offset + @items - @outset
40
+ @prev = (@page - 1 unless @page == 1)
41
+ @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
32
42
  end
33
43
 
34
44
  # Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
35
- def series(size=@vars[:size])
36
- return [] if size.empty?
37
- raise VariableError.new(self), "expected 4 items >= 0 in :size; got #{size.inspect}" \
38
- unless size.size == 4 && size.all?{ |num| num >= 0 rescue false }
39
- # This algorithm (backported from pagy 4.3) is up to ~5x faster and ~2.3x lighter than the previous one
40
- left_gap_start = 1 + size[0]
41
- left_gap_end = @page - size[1] - 1
42
- right_gap_start = @page + size[2] + 1
43
- right_gap_end = @last - size[3]
44
- left_gap_end = right_gap_end if left_gap_end > right_gap_end
45
- right_gap_start = left_gap_start if left_gap_start > right_gap_start
46
- series = []
47
- start = 1
48
- if (left_gap_end - left_gap_start) > 0
49
- series.push(*start..(left_gap_start - 1), :gap)
50
- start = left_gap_end + 1
51
- end
52
- if (right_gap_end - right_gap_start) > 0
53
- series.push(*start..(right_gap_start - 1), :gap)
54
- start = right_gap_end + 1
45
+ def series(size=@vars[:size])
46
+ return [] if size.empty?
47
+ raise VariableError.new(self), "expected 4 items >= 0 in :size; got #{size.inspect}" \
48
+ unless size.size == 4 && size.all?{ |num| !num.negative? rescue false } # rubocop:disable Style/RescueModifier
49
+ # This algorithm is up to ~5x faster and ~2.3x lighter than the previous one (pagy < 4.3)
50
+ left_gap_start = 1 + size[0]
51
+ left_gap_end = @page - size[1] - 1
52
+ right_gap_start = @page + size[2] + 1
53
+ right_gap_end = @last - size[3]
54
+ left_gap_end = right_gap_end if left_gap_end > right_gap_end
55
+ right_gap_start = left_gap_start if left_gap_start > right_gap_start
56
+ series = []
57
+ start = 1
58
+ if (left_gap_end - left_gap_start).positive?
59
+ series.push(*start..(left_gap_start - 1), :gap)
60
+ start = left_gap_end + 1
61
+ end
62
+ if (right_gap_end - right_gap_start).positive?
63
+ series.push(*start..(right_gap_start - 1), :gap)
64
+ start = right_gap_end + 1
65
+ end
66
+ series.push(*start..@last)
67
+ series[series.index(@page)] = @page.to_s
68
+ series
55
69
  end
56
- series.push(*start..@last)
57
- series[series.index(@page)] = @page.to_s
58
- series
59
- end
60
70
 
61
71
  end
62
72
 
73
+ require 'pagy/deprecation'
63
74
  require 'pagy/backend'
64
75
  require 'pagy/frontend'
65
76
  require 'pagy/exceptions'