pagy 6.0.0 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/lib/config/pagy.rb +56 -44
  4. data/lib/javascripts/pagy-dev.js +3 -3
  5. data/lib/javascripts/pagy-module.js +2 -2
  6. data/lib/javascripts/pagy.js +1 -1
  7. data/lib/locales/ar.yml +15 -11
  8. data/lib/locales/be.yml +25 -0
  9. data/lib/locales/bg.yml +10 -11
  10. data/lib/locales/bs.yml +12 -11
  11. data/lib/locales/ca.yml +12 -11
  12. data/lib/locales/cs.yml +13 -10
  13. data/lib/locales/da.yml +14 -13
  14. data/lib/locales/de.yml +10 -11
  15. data/lib/locales/en.yml +10 -11
  16. data/lib/locales/es.yml +10 -11
  17. data/lib/locales/fr.yml +10 -11
  18. data/lib/locales/hr.yml +12 -11
  19. data/lib/locales/id.yml +10 -11
  20. data/lib/locales/it.yml +10 -11
  21. data/lib/locales/ja.yml +10 -11
  22. data/lib/locales/km.yml +10 -10
  23. data/lib/locales/ko.yml +10 -11
  24. data/lib/locales/nb.yml +10 -11
  25. data/lib/locales/nl.yml +10 -11
  26. data/lib/locales/nn.yml +10 -11
  27. data/lib/locales/pl.yml +12 -11
  28. data/lib/locales/pt-BR.yml +10 -11
  29. data/lib/locales/pt.yml +10 -11
  30. data/lib/locales/ru.yml +14 -11
  31. data/lib/locales/sr.yml +12 -10
  32. data/lib/locales/sv-SE.yml +10 -12
  33. data/lib/locales/sv.yml +10 -12
  34. data/lib/locales/sw.yml +12 -11
  35. data/lib/locales/ta.yml +12 -11
  36. data/lib/locales/tr.yml +10 -11
  37. data/lib/locales/uk.yml +12 -11
  38. data/lib/locales/vi.yml +17 -0
  39. data/lib/locales/zh-CN.yml +10 -11
  40. data/lib/locales/zh-HK.yml +10 -11
  41. data/lib/locales/zh-TW.yml +10 -11
  42. data/lib/pagy/backend.rb +10 -4
  43. data/lib/pagy/calendar/day.rb +4 -3
  44. data/lib/pagy/calendar/helper.rb +15 -11
  45. data/lib/pagy/calendar/month.rb +4 -3
  46. data/lib/pagy/calendar/quarter.rb +4 -3
  47. data/lib/pagy/calendar/week.rb +1 -1
  48. data/lib/pagy/calendar/year.rb +3 -2
  49. data/lib/pagy/calendar.rb +33 -7
  50. data/lib/pagy/console.rb +1 -1
  51. data/lib/pagy/countless.rb +1 -1
  52. data/lib/pagy/exceptions.rb +1 -1
  53. data/lib/pagy/extras/arel.rb +1 -1
  54. data/lib/pagy/extras/array.rb +1 -1
  55. data/lib/pagy/extras/bootstrap.rb +45 -32
  56. data/lib/pagy/extras/bulma.rb +41 -32
  57. data/lib/pagy/extras/calendar.rb +17 -13
  58. data/lib/pagy/extras/countless.rb +1 -1
  59. data/lib/pagy/extras/elasticsearch_rails.rb +8 -8
  60. data/lib/pagy/extras/foundation.rb +48 -33
  61. data/lib/pagy/extras/frontend_helpers.rb +11 -11
  62. data/lib/pagy/extras/gearbox.rb +1 -1
  63. data/lib/pagy/extras/headers.rb +8 -8
  64. data/lib/pagy/extras/i18n.rb +5 -5
  65. data/lib/pagy/extras/items.rb +19 -12
  66. data/lib/pagy/extras/jsonapi.rb +79 -0
  67. data/lib/pagy/extras/materialize.rb +42 -30
  68. data/lib/pagy/extras/meilisearch.rb +12 -11
  69. data/lib/pagy/extras/metadata.rb +2 -2
  70. data/lib/pagy/extras/navs.rb +22 -34
  71. data/lib/pagy/extras/overflow.rb +9 -7
  72. data/lib/pagy/extras/searchkick.rb +8 -8
  73. data/lib/pagy/extras/semantic.rb +41 -28
  74. data/lib/pagy/extras/standalone.rb +10 -16
  75. data/lib/pagy/extras/support.rb +19 -33
  76. data/lib/pagy/extras/trim.rb +4 -4
  77. data/lib/pagy/extras/uikit.rb +44 -31
  78. data/lib/pagy/frontend.rb +57 -26
  79. data/lib/pagy/i18n.rb +3 -3
  80. data/lib/pagy/url_helpers.rb +15 -12
  81. data/lib/pagy.rb +57 -41
  82. metadata +7 -19
  83. data/lib/templates/bootstrap_nav.html.erb +0 -24
  84. data/lib/templates/bootstrap_nav.html.haml +0 -34
  85. data/lib/templates/bootstrap_nav.html.slim +0 -34
  86. data/lib/templates/bulma_nav.html.erb +0 -24
  87. data/lib/templates/bulma_nav.html.haml +0 -32
  88. data/lib/templates/bulma_nav.html.slim +0 -32
  89. data/lib/templates/foundation_nav.html.erb +0 -24
  90. data/lib/templates/foundation_nav.html.haml +0 -34
  91. data/lib/templates/foundation_nav.html.slim +0 -34
  92. data/lib/templates/nav.html.erb +0 -22
  93. data/lib/templates/nav.html.haml +0 -30
  94. data/lib/templates/nav.html.slim +0 -29
  95. data/lib/templates/uikit_nav.html.erb +0 -15
  96. data/lib/templates/uikit_nav.html.haml +0 -28
  97. data/lib/templates/uikit_nav.html.slim +0 -28
data/lib/locales/sv.yml CHANGED
@@ -1,23 +1,21 @@
1
- # :one_two_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
2
-
1
+ # :one_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
3
2
  sv:
4
3
  pagy:
5
-
4
+ aria_label:
5
+ nav:
6
+ one: "Sida"
7
+ other: "Sidor"
8
+ prev: "Föregående"
9
+ next: "Nästa"
10
+ prev: "<"
11
+ next: ">"
12
+ gap: "…"
6
13
  item_name:
7
14
  one: "resultat"
8
- two: "resultat"
9
15
  other: "resultat"
10
-
11
- nav:
12
- prev: "‹ Föregående"
13
- next: "Nästa ›"
14
- gap: "…"
15
-
16
16
  info:
17
17
  no_items: "Inga %{item_name} hittade"
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
-
21
20
  combo_nav_js: "<label>Sida %{page_input} av %{pages}</label>"
22
-
23
21
  items_selector_js: "<label>Visa %{items_input} %{item_name} per sida</label>"
data/lib/locales/sw.yml CHANGED
@@ -1,22 +1,23 @@
1
- # :one_other pluralization
2
-
1
+ # :one_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
3
2
  sw:
4
3
  pagy:
5
-
4
+ # please add a comment in the https://github.com/ddnexus/pagy/issues/603
5
+ # posting the translation of the following "Page"/"Pages" with the plurals for this locale
6
+ aria_label:
7
+ nav: "Pages"
8
+ # one: ""
9
+ # other: ""
10
+ prev: "Awali"
11
+ next: "Ifuatayo"
12
+ prev: "&lt;"
13
+ next: "&gt;"
14
+ gap: "&hellip;"
6
15
  item_name:
7
16
  one: "kifaa"
8
17
  other: "vifaa"
9
-
10
- nav:
11
- prev: "&lsaquo;&nbsp;Awali"
12
- next: "Ifuatayo&nbsp;&rsaquo;"
13
- gap: "&hellip;"
14
-
15
18
  info:
16
19
  no_items: "Hamna %{item_name} vilivyopatikana"
17
20
  single_page: "Inaonyesha %{item_name} <b>%{count}</b>"
18
21
  multiple_pages: "Inaonyesha %{item_name} <b>%{from}-%{to}</b> ya <b>%{count}</b> kwa jumla"
19
-
20
22
  combo_nav_js: "<label>Kurasa %{page_input} ya %{pages}</label>"
21
-
22
23
  items_selector_js: "<label>Onyesha %{items_input} %{item_name} kwa kila ukurasa</label>"
data/lib/locales/ta.yml CHANGED
@@ -1,22 +1,23 @@
1
- # :one_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
2
-
1
+ # :one_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
3
2
  ta:
4
3
  pagy:
5
-
4
+ aria_label:
5
+ # please add a comment in the https://github.com/ddnexus/pagy/issues/604
6
+ # posting the translation of the following "Page"/"Pages" with the plurals for this locale
7
+ nav: "Pages"
8
+ # one: ""
9
+ # other: ""
10
+ prev: "முந்தையது"
11
+ next: "அடுத்தது"
12
+ prev: "&lt;"
13
+ next: "&gt;"
14
+ gap: "&hellip;"
6
15
  item_name:
7
16
  one: "பதிவு"
8
17
  other: "பதிவுகள்"
9
-
10
- nav:
11
- prev: "&lsaquo;&nbsp;முந்தையது"
12
- next: "அடுத்தது&nbsp;&rsaquo;"
13
- gap: "&hellip;"
14
-
15
18
  info:
16
19
  no_items: "%{item_name} கிடைக்கவில்லை"
17
20
  single_page: "<b>%{count}</b> %{item_name} காட்டப்படுகின்றது"
18
21
  multiple_pages: "மொத்தம் <b>%{count}</b> %{item_name}, காட்டப்படுபவை <b>%{from}-%{to}</b>"
19
-
20
22
  combo_nav_js: "<label>%{pages}-இல் %{page_input}-வது பக்கம்</label>"
21
-
22
23
  items_selector_js: "<label>ஒரு பக்கத்திற்கு %{items_input} %{item_name} காட்டு</label>"
data/lib/locales/tr.yml CHANGED
@@ -1,20 +1,19 @@
1
- # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
2
-
1
+ # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
3
2
  tr:
4
3
  pagy:
5
-
4
+ aria_label:
5
+ # please add a comment in the https://github.com/ddnexus/pagy/issues/605
6
+ # posting the translation of the following "Page"/"Pages" with the plurals for this locale
7
+ nav: "Pages"
8
+ prev: "Önceki"
9
+ next: "Sonraki"
10
+ prev: "&lt;"
11
+ next: "&gt;"
12
+ gap: "&hellip;"
6
13
  item_name: "kayıt"
7
-
8
- nav:
9
- prev: "&lsaquo;&nbsp;Önceki"
10
- next: "Sonraki&nbsp;&rsaquo;"
11
- gap: "&hellip;"
12
-
13
14
  info:
14
15
  no_items: "Hiç %{item_name} bulunamadı"
15
16
  single_page: "Toplam <b>%{count}</b> %{item_name} gösteriliyor"
16
17
  multiple_pages: "<b>%{count}</b> %{item_name} içerisinden <b>%{from}-%{to}</b> kadarı gösteriliyor"
17
-
18
18
  combo_nav_js: "<label>Sayfa %{page_input} / %{pages}</label>"
19
-
20
19
  items_selector_js: "<label>Sayfada %{items_input} %{item_name} göster</label>"
data/lib/locales/uk.yml CHANGED
@@ -1,24 +1,25 @@
1
- # :east_slavic pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
2
-
1
+ # :east_slavic pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
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: "&lt;"
13
+ next: "&gt;"
14
+ gap: "&hellip;"
6
15
  item_name:
7
16
  one: "запис"
8
17
  few: "записів"
9
18
  many: "записів"
10
19
  other: "записів"
11
-
12
- nav:
13
- prev: "&lsaquo;&nbsp;Назад"
14
- next: "Далі&nbsp;&rsaquo;"
15
- gap: "&hellip;"
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>"
@@ -0,0 +1,17 @@
1
+ # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
2
+ vi:
3
+ pagy:
4
+ aria_label:
5
+ nav: "Trang"
6
+ prev: "Trang trước"
7
+ next: "Trang sau"
8
+ prev: "&lt;"
9
+ next: "&gt;"
10
+ gap: "&hellip;"
11
+ item_name: "kết quả"
12
+ info:
13
+ no_items: "Không tìm thấy %{item_name} nào"
14
+ single_page: "<b>%{count}</b> %{item_name}"
15
+ multiple_pages: "Đang hiển thị <b>%{from}-%{to}</b> trong tổng số <b>%{count} %{item_name}</b>"
16
+ combo_nav_js: "<label>Trang %{page_input} trên %{pages}</label>"
17
+ items_selector_js: "<label>Hiển thị %{items_input} %{item_name} mỗi trang</label>"
@@ -1,20 +1,19 @@
1
- # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
2
-
1
+ # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
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
- # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
2
-
1
+ # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
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
- # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
2
-
1
+ # :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
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
@@ -1,8 +1,8 @@
1
- # See Pagy::Backend API documentation: https://ddnexus.github.io/pagy/api/backend
1
+ # See Pagy::Backend API documentation: https://ddnexus.github.io/pagy/docs/api/backend
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,11 +18,17 @@ 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
22
- vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
21
+ vars[:count] ||= (count = collection.count(*vars[:count_args])).is_a?(Hash) ? count.size : count
22
+ vars[:page] ||= pagy_get_page(vars)
23
23
  vars
24
24
  end
25
25
 
26
+ # Get the page integer from the params
27
+ # Overridable by the jsonapi extra
28
+ def pagy_get_page(vars)
29
+ (params[vars[:page_param] || DEFAULT[:page_param]] || 1).to_i
30
+ end
31
+
26
32
  # Sub-method called only by #pagy: here for easy customization of record-extraction by overriding
27
33
  # You may need to override this method for collections without offset|limit
28
34
  def pagy_get_items(collection, pagy)
@@ -1,12 +1,13 @@
1
- # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
4
  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
 
@@ -3,8 +3,7 @@
3
3
  class Pagy # :nodoc:
4
4
  class Calendar # :nodoc:
5
5
  # Initializes the calendar objects, reducing complexity in the extra
6
- # The returned calendar is a simple hash of units/objects with an added helper
7
- # returning the last_object_at(time) used in the extra
6
+ # The returned calendar is a simple hash of units/objects
8
7
  class Helper < Hash
9
8
  class << self
10
9
  private
@@ -16,14 +15,15 @@ class Pagy # :nodoc:
16
15
 
17
16
  private
18
17
 
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,29 +33,33 @@ 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
44
40
  [replace(calendar), object.from, object.to]
45
41
  end
46
42
 
47
- def last_object_at(time)
43
+ # Return the calendar object at time
44
+ def calendar_at(time, **opts)
48
45
  conf = Marshal.load(Marshal.dump(@conf))
49
46
  page_params = {}
50
47
  @units.inject(nil) do |object, unit|
51
48
  conf[unit][:period] = object&.send(:active_period) || @period
52
49
  conf[unit][:page] = page_params[:"#{unit}_#{@page_param}"] \
53
- = Calendar.send(:create, unit, conf[unit]).send(:page_at, time)
50
+ = Calendar.send(:create, unit, conf[unit]).send(:page_at, time, **opts)
54
51
  conf[unit][:params] ||= {}
55
52
  conf[unit][:params].merge!(page_params)
56
53
  Calendar.send(:create, unit, conf[unit])
57
54
  end
58
55
  end
56
+
57
+ public
58
+
59
+ # Return the current time of the smallest time unit shown
60
+ def showtime
61
+ self[@units.last].from
62
+ end
59
63
  end
60
64
  end
61
65
  end
@@ -1,12 +1,13 @@
1
- # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
4
  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
 
@@ -1,12 +1,13 @@
1
- # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
4
  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 = {})
@@ -1,4 +1,4 @@
1
- # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class Pagy # :nodoc:
@@ -1,11 +1,12 @@
1
- # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
4
  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
data/lib/pagy/calendar.rb CHANGED
@@ -1,4 +1,4 @@
1
- # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'active_support'
@@ -13,12 +13,12 @@ class Pagy # :nodoc:
13
13
  # Base class for time units subclasses (Year, Quarter, Month, Week, Day)
14
14
  class Calendar < Pagy
15
15
  # Specific out of range error
16
- class OutOfRangeError < StandardError; end
16
+ class OutOfRangeError < VariableError; end
17
17
 
18
18
  # List of units in desc order of duration. It can be used for custom units.
19
19
  UNITS = %i[year quarter month week day] # rubocop:disable Style/MutableConstant
20
20
 
21
- attr_reader :order
21
+ attr_reader :order, :from, :to
22
22
 
23
23
  # Merge and validate the options, do some simple arithmetic and set a few instance variables
24
24
  def initialize(vars) # rubocop:disable Lint/MissingSuper
@@ -49,10 +49,24 @@ class Pagy # :nodoc:
49
49
  protected
50
50
 
51
51
  # The page that includes time
52
- def page_at(time)
53
- raise OutOfRangeError unless time.between?(@initial, @final)
54
-
55
- offset = page_offset_at(time) # offset starts from 0
52
+ # In case of out of range time, the :fit_time option avoids the outOfRangeError
53
+ # and returns the closest page to the passed time argument (first or last page)
54
+ def page_at(time, **opts)
55
+ fit_time = time
56
+ fit_final = @final - 1
57
+ unless time.between?(@initial, fit_final)
58
+ raise OutOfRangeError.new(self, :time, "between #{@initial} and #{fit_final}", time) unless opts[:fit_time]
59
+
60
+ if time < @final
61
+ fit_time = @initial
62
+ ordinal = 'first'
63
+ else
64
+ fit_time = fit_final
65
+ ordinal = 'last'
66
+ end
67
+ Warning.warn "Pagy::Calendar#page_at: Rescued #{time} out of range by returning the #{ordinal} page."
68
+ end
69
+ offset = page_offset_at(fit_time) # offset starts from 0
56
70
  @order == :asc ? offset + 1 : @pages - offset
57
71
  end
58
72
 
@@ -84,6 +98,18 @@ class Pagy # :nodoc:
84
98
  [[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
85
99
  end
86
100
 
101
+ # :nocov:
102
+ # This method must be implemented by the unit subclass
103
+ def starting_time_for(*)
104
+ raise NoMethodError, 'the starting_time_for method must be implemented by the unit subclass'
105
+ end
106
+
107
+ # This method must be implemented by the unit subclass
108
+ def page_offset_at(*)
109
+ raise NoMethodError, 'the page_offset_at method must be implemented by the unit subclass'
110
+ end
111
+ # :nocov:
112
+
87
113
  class << self
88
114
  # Create a subclass instance by unit name (internal use)
89
115
  def create(unit, vars)
data/lib/pagy/console.rb CHANGED
@@ -1,4 +1,4 @@
1
- # See Pagy::Console API documentation: https://ddnexus.github.io/pagy/api/console
1
+ # See Pagy::Console API documentation: https://ddnexus.github.io/pagy/docs/api/console
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'pagy' # so you can require just the extra in the console
@@ -1,4 +1,4 @@
1
- # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/countless
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/countless
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'pagy'
@@ -10,7 +10,7 @@ class Pagy
10
10
  @pagy = pagy
11
11
  @variable = variable
12
12
  @value = value
13
- super "expected :#{@variable} #{description}; got #{@value.inspect}"
13
+ super("expected :#{@variable} #{description}; got #{@value.inspect}")
14
14
  end
15
15
  end
16
16
 
@@ -1,4 +1,4 @@
1
- # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/arel
1
+ # See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/arel
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class Pagy # :nodoc:
@@ -1,4 +1,4 @@
1
- # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/array
1
+ # See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/array
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class Pagy # :nodoc: