pagy 8.4.0 → 9.0.0

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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/apps/calendar.ru +682 -2137
  3. data/apps/demo.ru +17 -13
  4. data/apps/keyset_ar.ru +236 -0
  5. data/apps/keyset_s.ru +238 -0
  6. data/apps/rails.ru +19 -15
  7. data/apps/repro.ru +17 -14
  8. data/apps/tmp/calendar.sqlite3 +0 -0
  9. data/apps/tmp/calendar.sqlite3-shm +0 -0
  10. data/apps/tmp/calendar.sqlite3-wal +0 -0
  11. data/apps/tmp/local_secret.txt +1 -0
  12. data/apps/tmp/pagy-keyset-ar.sqlite3 +0 -0
  13. data/apps/tmp/pagy-keyset-ar.sqlite3-shm +0 -0
  14. data/apps/tmp/pagy-keyset-ar.sqlite3-wal +0 -0
  15. data/apps/tmp/pagy-keyset-s.sqlite3 +0 -0
  16. data/bin/pagy +17 -12
  17. data/config/pagy.rb +32 -33
  18. data/javascripts/pagy-module.js +94 -107
  19. data/javascripts/pagy.js +4 -1
  20. data/javascripts/pagy.min.js +4 -0
  21. data/javascripts/pagy.min.js.map +10 -0
  22. data/javascripts/pagy.mjs +100 -0
  23. data/lib/optimist.rb +1 -1
  24. data/lib/pagy/b64.rb +33 -0
  25. data/lib/pagy/backend.rb +21 -17
  26. data/lib/pagy/calendar/day.rb +4 -3
  27. data/lib/pagy/calendar/month.rb +4 -3
  28. data/lib/pagy/calendar/quarter.rb +4 -3
  29. data/lib/pagy/calendar/unit.rb +103 -0
  30. data/lib/pagy/calendar/week.rb +3 -3
  31. data/lib/pagy/calendar/year.rb +4 -3
  32. data/lib/pagy/calendar.rb +54 -97
  33. data/lib/pagy/countless.rb +15 -16
  34. data/lib/pagy/extras/arel.rb +8 -10
  35. data/lib/pagy/extras/array.rb +4 -6
  36. data/lib/pagy/extras/bootstrap.rb +5 -5
  37. data/lib/pagy/extras/bulma.rb +10 -7
  38. data/lib/pagy/extras/calendar.rb +34 -5
  39. data/lib/pagy/extras/countless.rb +6 -13
  40. data/lib/pagy/extras/elasticsearch_rails.rb +15 -15
  41. data/lib/pagy/extras/gearbox.rb +26 -26
  42. data/lib/pagy/extras/headers.rb +25 -24
  43. data/lib/pagy/extras/i18n.rb +1 -1
  44. data/lib/pagy/extras/js_tools.rb +9 -9
  45. data/lib/pagy/extras/jsonapi.rb +26 -16
  46. data/lib/pagy/extras/keyset.rb +26 -0
  47. data/lib/pagy/extras/limit.rb +63 -0
  48. data/lib/pagy/extras/meilisearch.rb +11 -11
  49. data/lib/pagy/extras/metadata.rb +6 -2
  50. data/lib/pagy/extras/overflow.rb +9 -8
  51. data/lib/pagy/extras/pagy.rb +16 -16
  52. data/lib/pagy/extras/searchkick.rb +11 -11
  53. data/lib/pagy/extras/size.rb +40 -0
  54. data/lib/pagy/extras/standalone.rb +6 -6
  55. data/lib/pagy/extras/trim.rb +3 -3
  56. data/lib/pagy/frontend.rb +38 -36
  57. data/lib/pagy/i18n.rb +1 -1
  58. data/lib/pagy/keyset/active_record.rb +38 -0
  59. data/lib/pagy/keyset/sequel.rb +51 -0
  60. data/lib/pagy/keyset.rb +99 -0
  61. data/lib/pagy/url_helpers.rb +5 -5
  62. data/lib/pagy.rb +92 -94
  63. data/locales/ar.yml +9 -10
  64. data/locales/be.yml +2 -2
  65. data/locales/bg.yml +2 -2
  66. data/locales/bs.yml +2 -2
  67. data/locales/ca.yml +5 -7
  68. data/locales/ckb.yml +2 -2
  69. data/locales/cs.yml +2 -2
  70. data/locales/da.yml +2 -2
  71. data/locales/de.yml +2 -2
  72. data/locales/en.yml +2 -2
  73. data/locales/es.yml +2 -2
  74. data/locales/fr.yml +2 -2
  75. data/locales/hr.yml +2 -2
  76. data/locales/id.yml +2 -2
  77. data/locales/it.yml +2 -2
  78. data/locales/ja.yml +2 -2
  79. data/locales/km.yml +2 -2
  80. data/locales/ko.yml +2 -2
  81. data/locales/nb.yml +2 -2
  82. data/locales/nl.yml +2 -2
  83. data/locales/nn.yml +2 -2
  84. data/locales/pl.yml +2 -2
  85. data/locales/pt-BR.yml +2 -2
  86. data/locales/pt.yml +2 -2
  87. data/locales/ru.yml +2 -2
  88. data/locales/sr.yml +2 -2
  89. data/locales/sv-SE.yml +2 -2
  90. data/locales/sv.yml +2 -2
  91. data/locales/sw.yml +2 -2
  92. data/locales/ta.yml +2 -2
  93. data/locales/tr.yml +2 -2
  94. data/locales/uk.yml +2 -2
  95. data/locales/vi.yml +2 -2
  96. data/locales/zh-CN.yml +2 -2
  97. data/locales/zh-HK.yml +2 -2
  98. data/locales/zh-TW.yml +2 -2
  99. data/pkg/pagy-9.0.0.gem +0 -0
  100. metadata +27 -17
  101. data/javascripts/pagy-dev.js +0 -114
  102. data/lib/pagy/calendar/helper.rb +0 -65
  103. data/lib/pagy/extras/foundation.rb +0 -95
  104. data/lib/pagy/extras/items.rb +0 -64
  105. data/lib/pagy/extras/materialize.rb +0 -100
  106. data/lib/pagy/extras/semantic.rb +0 -94
  107. data/lib/pagy/extras/uikit.rb +0 -98
  108. /data/javascripts/{pagy-module.d.ts → pagy.d.ts} +0 -0
@@ -0,0 +1,100 @@
1
+ const Pagy = (() => {
2
+ const rjsObserver = new ResizeObserver((entries) => entries.forEach((e) => e.target.querySelectorAll(".pagy-rjs").forEach((el) => el.pagyRender())));
3
+ const initNav = (el, [tokens, sequels, labelSequels, trimParam]) => {
4
+ const container = el.parentElement ?? el;
5
+ const widths = Object.keys(sequels).map((w) => parseInt(w)).sort((a, b) => b - a);
6
+ let lastWidth = -1;
7
+ const fillIn = (a, page, label) => a.replace(/__pagy_page__/g, page).replace(/__pagy_label__/g, label);
8
+ (el.pagyRender = function() {
9
+ const width = widths.find((w) => w < container.clientWidth) || 0;
10
+ if (width === lastWidth) {
11
+ return;
12
+ }
13
+ let html = tokens.before;
14
+ const series = sequels[width.toString()];
15
+ const labels = labelSequels?.[width.toString()] ?? series.map((l) => l.toString());
16
+ series.forEach((item, i) => {
17
+ const label = labels[i];
18
+ let filled;
19
+ if (typeof item === "number") {
20
+ filled = fillIn(tokens.a, item.toString(), label);
21
+ } else if (item === "gap") {
22
+ filled = tokens.gap;
23
+ } else {
24
+ filled = fillIn(tokens.current, item, label);
25
+ }
26
+ html += typeof trimParam === "string" && item == 1 ? trim(filled, trimParam) : filled;
27
+ });
28
+ html += tokens.after;
29
+ el.innerHTML = "";
30
+ el.insertAdjacentHTML("afterbegin", html);
31
+ lastWidth = width;
32
+ })();
33
+ if (el.classList.contains("pagy-rjs")) {
34
+ rjsObserver.observe(container);
35
+ }
36
+ };
37
+ const initCombo = (el, [url_token, trimParam]) => initInput(el, (inputValue) => [inputValue, url_token.replace(/__pagy_page__/, inputValue)], trimParam);
38
+ const initSelector = (el, [from, url_token, trimParam]) => {
39
+ initInput(el, (inputValue) => {
40
+ const page = Math.max(Math.ceil(from / parseInt(inputValue)), 1).toString();
41
+ const url = url_token.replace(/__pagy_page__/, page).replace(/__pagy_limit__/, inputValue);
42
+ return [page, url];
43
+ }, trimParam);
44
+ };
45
+ const initInput = (el, getVars, trimParam) => {
46
+ const input = el.querySelector("input");
47
+ const link = el.querySelector("a");
48
+ const initial = input.value;
49
+ const action = function() {
50
+ if (input.value === initial) {
51
+ return;
52
+ }
53
+ const [min, val, max] = [input.min, input.value, input.max].map((n) => parseInt(n) || 0);
54
+ if (val < min || val > max) {
55
+ input.value = initial;
56
+ input.select();
57
+ return;
58
+ }
59
+ let [page, url] = getVars(input.value);
60
+ if (typeof trimParam === "string" && page === "1") {
61
+ url = trim(url, trimParam);
62
+ }
63
+ link.href = url;
64
+ link.click();
65
+ };
66
+ ["change", "focus"].forEach((e) => input.addEventListener(e, () => input.select()));
67
+ input.addEventListener("focusout", action);
68
+ input.addEventListener("keypress", (e) => {
69
+ if (e.key === "Enter") {
70
+ action();
71
+ }
72
+ });
73
+ };
74
+ const trim = (a, param) => a.replace(new RegExp(`[?&]${param}=1\\b(?!&)|\\b${param}=1&`), "");
75
+ return {
76
+ version: "9.0.0",
77
+ init(arg) {
78
+ const target = arg instanceof Element ? arg : document;
79
+ const elements = target.querySelectorAll("[data-pagy]");
80
+ for (const el of elements) {
81
+ try {
82
+ const uint8array = Uint8Array.from(atob(el.getAttribute("data-pagy")), (c) => c.charCodeAt(0));
83
+ const [keyword, ...args] = JSON.parse(new TextDecoder().decode(uint8array));
84
+ if (keyword === "nav") {
85
+ initNav(el, args);
86
+ } else if (keyword === "combo") {
87
+ initCombo(el, args);
88
+ } else if (keyword === "selector") {
89
+ initSelector(el, args);
90
+ } else {
91
+ console.warn("Skipped Pagy.init() for: %o\nUnknown keyword '%s'", el, keyword);
92
+ }
93
+ } catch (err) {
94
+ console.warn("Skipped Pagy.init() for: %o\n%s", el, err);
95
+ }
96
+ }
97
+ }
98
+ };
99
+ })();
100
+ export default Pagy;
data/lib/optimist.rb CHANGED
@@ -137,7 +137,7 @@ module Optimist
137
137
  ## There's one ambiguous case to be aware of: when +:multi+: is true and a
138
138
  ## +:default+ is set to an array (of something), it's ambiguous whether this
139
139
  ## is a multi-value argument as well as a multi-occurrence argument.
140
- ## In thise case, Optimist assumes that it's not a multi-value argument.
140
+ ## In this case, Optimist assumes that it's not a multi-value argument.
141
141
  ## If you want a multi-value, multi-occurrence argument with a default
142
142
  ## value, you must specify +:type+ as well.
143
143
 
data/lib/pagy/b64.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pagy # :nodoc:
4
+ # Cheap Base64 specialized methods to avoid dependencies
5
+ module B64
6
+ module_function
7
+
8
+ def encode(bin)
9
+ [bin].pack('m0')
10
+ end
11
+
12
+ def decode(str)
13
+ str.unpack1('m0')
14
+ end
15
+
16
+ def urlsafe_encode(bin)
17
+ str = encode(bin)
18
+ str.chomp!('==') or str.chomp!('=')
19
+ str.tr!('+/', '-_')
20
+ str
21
+ end
22
+
23
+ def urlsafe_decode(str)
24
+ if !str.end_with?('=') && str.length % 4 != 0
25
+ str = str.ljust((str.length + 3) & ~3, '=')
26
+ str.tr!('-_', '+/')
27
+ else
28
+ str = str.tr('-_', '+/')
29
+ end
30
+ decode(str)
31
+ end
32
+ end
33
+ end
data/lib/pagy/backend.rb CHANGED
@@ -8,37 +8,41 @@ class Pagy
8
8
  module Backend
9
9
  private
10
10
 
11
- # Return Pagy object and paginated items/results
12
- def pagy(collection, vars = {})
13
- pagy = Pagy.new(pagy_get_vars(collection, vars))
11
+ # Return Pagy object and paginated results
12
+ def pagy(collection, **vars)
13
+ pagy = Pagy.new(**pagy_get_vars(collection, vars))
14
14
  [pagy, pagy_get_items(collection, pagy)]
15
15
  end
16
16
 
17
- # Sub-method called only by #pagy: here for easy customization of variables by overriding
18
- # You may need to override the count call for non AR collections
19
- def pagy_get_vars(collection, vars)
20
- pagy_set_items_from_params(vars) if defined?(ItemsExtra)
21
- vars[:count] ||= pagy_get_count(collection, vars)
22
- vars[:page] ||= pagy_get_page(vars)
23
- vars
24
- end
25
-
26
17
  # Get the count from the collection
27
18
  def pagy_get_count(collection, vars)
28
19
  count_args = vars[:count_args] || DEFAULT[:count_args]
29
20
  (count = collection.count(*count_args)).is_a?(Hash) ? count.size : count
30
21
  end
31
22
 
23
+ # Sub-method called only by #pagy: here for easy customization of fetching by overriding
24
+ # You may need to override this method for collections without offset|limit
25
+ def pagy_get_items(collection, pagy)
26
+ collection.offset(pagy.offset).limit(pagy.limit)
27
+ end
28
+
29
+ # Override for limit extra
30
+ def pagy_get_limit(vars); end
31
+
32
32
  # Get the page integer from the params
33
33
  # Overridable by the jsonapi extra
34
34
  def pagy_get_page(vars)
35
- [params[vars[:page_param] || DEFAULT[:page_param]].to_i, 1].max
35
+ params[vars[:page_param] || DEFAULT[:page_param]]
36
36
  end
37
37
 
38
- # Sub-method called only by #pagy: here for easy customization of record-extraction by overriding
39
- # You may need to override this method for collections without offset|limit
40
- def pagy_get_items(collection, pagy)
41
- collection.offset(pagy.offset).limit(pagy.in)
38
+ # Sub-method called only by #pagy: here for easy customization of variables by overriding
39
+ # You may need to override the count call for non AR collections
40
+ def pagy_get_vars(collection, vars)
41
+ vars.tap do |v|
42
+ v[:count] ||= pagy_get_count(collection, v)
43
+ v[:limit] ||= pagy_get_limit(v)
44
+ v[:page] ||= pagy_get_page(v)
45
+ end
42
46
  end
43
47
  end
44
48
  end
@@ -3,16 +3,17 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar day subclass
7
- class Day < Calendar
6
+ # Day unit subclass
7
+ class Day < Unit
8
8
  DEFAULT = { size: 31, # rubocop:disable Style/MutableConstant
9
+ ends: false,
9
10
  order: :asc,
10
11
  format: '%d' }
11
12
 
12
13
  protected
13
14
 
14
15
  # Setup the calendar variables
15
- def setup_unit_vars
16
+ def assign_unit_vars
16
17
  super
17
18
  @initial = @starting.beginning_of_day
18
19
  @final = @ending.tomorrow.beginning_of_day
@@ -3,16 +3,17 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar month subclass
7
- class Month < Calendar
6
+ # Month unit subclass
7
+ class Month < Unit
8
8
  DEFAULT = { size: 12, # rubocop:disable Style/MutableConstant
9
+ ends: false,
9
10
  order: :asc,
10
11
  format: '%b' }
11
12
 
12
13
  protected
13
14
 
14
15
  # Setup the calendar variables
15
- def setup_unit_vars
16
+ def assign_unit_vars
16
17
  super
17
18
  @initial = @starting.beginning_of_month
18
19
  @final = @ending.next_month.beginning_of_month
@@ -3,9 +3,10 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar quarter subclass
7
- class Quarter < Calendar
6
+ # Quarter unit subclass
7
+ class Quarter < Unit
8
8
  DEFAULT = { size: 4, # rubocop:disable Style/MutableConstant
9
+ ends: false,
9
10
  order: :asc,
10
11
  format: 'Q%q' } # '%q' token
11
12
 
@@ -19,7 +20,7 @@ class Pagy # :nodoc:
19
20
  protected
20
21
 
21
22
  # Setup the calendar variables
22
- def setup_unit_vars
23
+ def assign_unit_vars
23
24
  super
24
25
  @initial = @starting.beginning_of_quarter
25
26
  @final = @ending.next_quarter.beginning_of_quarter
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext/time'
5
+ require 'active_support/core_ext/date_and_time/calculations'
6
+ require 'active_support/core_ext/numeric/time'
7
+ require 'active_support/core_ext/integer/time'
8
+
9
+ class Pagy # :nodoc:
10
+ class Calendar < Hash # :nodoc:
11
+ # Base class for time units subclasses (Year, Quarter, Month, Week, Day)
12
+ class Unit < Pagy
13
+ attr_reader :order, :from, :to
14
+
15
+ # Merge and validate the options, do some simple arithmetic and set a few instance variables
16
+ def initialize(**vars) # rubocop:disable Lint/MissingSuper
17
+ raise InternalError, 'Pagy::Calendar::Unit is a base class; use one of its subclasses' \
18
+ if instance_of?(Pagy::Calendar::Unit)
19
+
20
+ assign_vars({ **Pagy::DEFAULT, **self.class::DEFAULT }, vars)
21
+ assign_and_check(page: 1)
22
+ assign_unit_vars
23
+ check_overflow
24
+ assign_prev_and_next
25
+ end
26
+
27
+ # The label for the current page (it can pass along the I18n gem opts when it's used with the i18n extra)
28
+ def label(opts = {})
29
+ label_for(@page, opts)
30
+ end
31
+
32
+ # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
33
+ def label_for(page, opts = {})
34
+ opts[:format] ||= @vars[:format]
35
+ localize(starting_time_for(page.to_i), opts) # page could be a string
36
+ end
37
+
38
+ protected
39
+
40
+ # The page that includes time
41
+ # In case of out of range time, the :fit_time option avoids the outOfRangeError
42
+ # and returns the closest page to the passed time argument (first or last page)
43
+ def page_at(time, **opts)
44
+ fit_time = time
45
+ fit_final = @final - 1
46
+ unless time.between?(@initial, fit_final)
47
+ raise OutOfRangeError.new(self, :time, "between #{@initial} and #{fit_final}", time) unless opts[:fit_time]
48
+
49
+ if time < @final
50
+ fit_time = @initial
51
+ ordinal = 'first'
52
+ else
53
+ fit_time = fit_final
54
+ ordinal = 'last'
55
+ end
56
+ warn "Pagy::Calendar#page_at: Rescued #{time} out of range by returning the #{ordinal} page."
57
+ end
58
+ offset = page_offset_at(fit_time) # offset starts from 0
59
+ @order == :asc ? offset + 1 : @last - offset
60
+ end
61
+
62
+ # Base class method for the setup of the unit variables (subclasses must implement it and call super)
63
+ def assign_unit_vars
64
+ raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
65
+ raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
66
+ unless %i[asc desc].include?(@order = @vars[:order])
67
+
68
+ @starting, @ending = @vars[:period]
69
+ raise VariableError.new(self, :period, 'to be a an Array of min and max TimeWithZone instances', @vars[:period]) \
70
+ unless @starting.is_a?(ActiveSupport::TimeWithZone) \
71
+ && @ending.is_a?(ActiveSupport::TimeWithZone) && @starting <= @ending
72
+ end
73
+
74
+ # Apply the strftime format to the time (overridden by the i18n extra when localization is required)
75
+ def localize(time, opts)
76
+ time.strftime(opts[:format])
77
+ end
78
+
79
+ # Number of time units to offset from the @initial time, in order to get the ordered starting time for the page.
80
+ # Used in starting_time_for(page) where page starts from 1 (e.g. page to starting_time means subtracting 1)
81
+ def time_offset_for(page)
82
+ @order == :asc ? page - 1 : @last - page
83
+ end
84
+
85
+ # Period of the active page (used internally for nested units)
86
+ def active_period
87
+ [[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
88
+ end
89
+
90
+ # :nocov:
91
+ # This method must be implemented by the unit subclass
92
+ def starting_time_for(*)
93
+ raise NoMethodError, 'the starting_time_for method must be implemented by the unit subclass'
94
+ end
95
+
96
+ # This method must be implemented by the unit subclass
97
+ def page_offset_at(*)
98
+ raise NoMethodError, 'the page_offset_at method must be implemented by the unit subclass'
99
+ end
100
+ # :nocov:
101
+ end
102
+ end
103
+ end
@@ -3,15 +3,15 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar week subclass
7
- class Week < Calendar
6
+ # Week unit subclass
7
+ class Week < Unit
8
8
  DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
9
  format: '%Y-%W' }
10
10
 
11
11
  protected
12
12
 
13
13
  # Setup the calendar variables
14
- def setup_unit_vars
14
+ def assign_unit_vars
15
15
  super
16
16
  @initial = @starting.beginning_of_week
17
17
  @final = @ending.next_week.beginning_of_week
@@ -3,16 +3,17 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar year subclass
7
- class Year < Calendar
6
+ # Year unit subclass
7
+ class Year < Unit
8
8
  DEFAULT = { size: 10, # rubocop:disable Style/MutableConstant
9
+ ends: false,
9
10
  order: :asc,
10
11
  format: '%Y' }
11
12
 
12
13
  protected
13
14
 
14
15
  # Setup the calendar variables
15
- def setup_unit_vars
16
+ def assign_unit_vars
16
17
  super
17
18
  @initial = @starting.beginning_of_year
18
19
  @final = @ending.next_year.beginning_of_year
data/lib/pagy/calendar.rb CHANGED
@@ -1,122 +1,79 @@
1
1
  # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'active_support'
5
- require 'active_support/core_ext/time'
6
- require 'active_support/core_ext/date_and_time/calculations'
7
- require 'active_support/core_ext/numeric/time'
8
- require 'active_support/core_ext/integer/time'
9
-
10
4
  require_relative '../pagy'
5
+ require_relative 'calendar/unit'
11
6
 
12
7
  class Pagy # :nodoc:
13
- # Base class for time units subclasses (Year, Quarter, Month, Week, Day)
14
- class Calendar < Pagy
8
+ # Calendar class
9
+ class Calendar < Hash
15
10
  # Specific out of range error
16
11
  class OutOfRangeError < VariableError; end
17
12
 
18
13
  # List of units in desc order of duration. It can be used for custom units.
19
14
  UNITS = %i[year quarter month week day] # rubocop:disable Style/MutableConstant
20
15
 
21
- attr_reader :order, :from, :to
22
-
23
- # Merge and validate the options, do some simple arithmetic and set a few instance variables
24
- def initialize(vars) # rubocop:disable Lint/MissingSuper
25
- raise InternalError, 'Pagy::Calendar is a base class; use one of its subclasses' if instance_of?(Pagy::Calendar)
26
-
27
- vars = self.class::DEFAULT.merge(vars) # subclass specific default
28
- normalize_vars(vars) # general default
29
- setup_vars(page: 1)
30
- setup_unit_vars
31
- raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
32
-
33
- @prev = (@page - 1 unless @page == 1)
34
- @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
35
- end
36
-
37
- # The label for the current page (it can pass along the I18n gem opts when it's used with the i18n extra)
38
- def label(opts = {})
39
- label_for(@page, opts)
40
- end
41
-
42
- # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
43
- def label_for(page, opts = {})
44
- opts[:format] ||= @vars[:format]
45
- localize(starting_time_for(page.to_i), opts) # page could be a string
46
- end
47
-
48
- protected
16
+ class << self
17
+ private
49
18
 
50
- # The page that includes time
51
- # In case of out of range time, the :fit_time option avoids the outOfRangeError
52
- # and returns the closest page to the passed time argument (first or last page)
53
- def page_at(time, **opts)
54
- fit_time = time
55
- fit_final = @final - 1
56
- unless time.between?(@initial, fit_final)
57
- raise OutOfRangeError.new(self, :time, "between #{@initial} and #{fit_final}", time) unless opts[:fit_time]
19
+ # Create a unit subclass instance by using the unit name (internal use)
20
+ def create(unit, **vars)
21
+ raise InternalError, "unit must be in #{UNITS.inspect}; got #{unit}" unless UNITS.include?(unit)
58
22
 
59
- if time < @final
60
- fit_time = @initial
61
- ordinal = 'first'
62
- else
63
- fit_time = fit_final
64
- ordinal = 'last'
65
- end
66
- Warning.warn "Pagy::Calendar#page_at: Rescued #{time} out of range by returning the #{ordinal} page."
23
+ name = unit.to_s
24
+ name[0] = name[0].capitalize
25
+ Object.const_get("Pagy::Calendar::#{name}").new(**vars)
67
26
  end
68
- offset = page_offset_at(fit_time) # offset starts from 0
69
- @order == :asc ? offset + 1 : @last - offset
70
- end
71
27
 
72
- # Base class method for the setup of the unit variables (subclasses must implement it and call super)
73
- def setup_unit_vars
74
- raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
75
- raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
76
- unless %i[asc desc].include?(@order = @vars[:order])
77
-
78
- @starting, @ending = @vars[:period]
79
- raise VariableError.new(self, :period, 'to be a an Array of min and max TimeWithZone instances', @vars[:period]) \
80
- unless @starting.is_a?(ActiveSupport::TimeWithZone) \
81
- && @ending.is_a?(ActiveSupport::TimeWithZone) && @starting <= @ending
82
- end
83
-
84
- # Apply the strftime format to the time (overridden by the i18n extra when localization is required)
85
- def localize(time, opts)
86
- time.strftime(opts[:format])
87
- end
88
-
89
- # Number of time units to offset from the @initial time, in order to get the ordered starting time for the page.
90
- # Used in starting_time_for(page) where page starts from 1 (e.g. page to starting_time means subtracting 1)
91
- def time_offset_for(page)
92
- @order == :asc ? page - 1 : @last - page
93
- end
94
-
95
- # Period of the active page (used internally for nested units)
96
- def active_period
97
- [[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
28
+ # Return calendar, from, to
29
+ def init(conf, period, params)
30
+ new.send(:init, conf, period, params)
31
+ end
98
32
  end
99
33
 
100
- # :nocov:
101
- # This method must be implemented by the unit subclass
102
- def starting_time_for(*)
103
- raise NoMethodError, 'the starting_time_for method must be implemented by the unit subclass'
34
+ # Return the current time of the smallest time unit shown
35
+ def showtime
36
+ self[@units.last].from
104
37
  end
105
38
 
106
- # This method must be implemented by the unit subclass
107
- def page_offset_at(*)
108
- raise NoMethodError, 'the page_offset_at method must be implemented by the unit subclass'
39
+ private
40
+
41
+ # Create the calendar
42
+ def init(conf, period, params)
43
+ @conf = Marshal.load(Marshal.dump(conf)) # store a copy
44
+ @units = Calendar::UNITS & @conf.keys # get the units in time length desc order
45
+ raise ArgumentError, 'no calendar unit found in pagy_calendar @configuration' if @units.empty?
46
+
47
+ @period = period
48
+ @params = params
49
+ @page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
50
+ @units.each do |unit| # set all the :page_param vars for later deletion
51
+ unit_page_param = :"#{unit}_#{@page_param}"
52
+ conf[unit][:page_param] = unit_page_param
53
+ conf[unit][:page] = @params[unit_page_param]
54
+ end
55
+ calendar = {}
56
+ object = nil
57
+ @units.each_with_index do |unit, index|
58
+ params_to_delete = @units[(index + 1), @units.size].map { |sub| conf[sub][:page_param] } + [@page_param]
59
+ conf[unit][:params] = lambda { |up| up.except(*params_to_delete.map(&:to_s)) } # rubocop:disable Style/Lambda
60
+ conf[unit][:period] = object&.send(:active_period) || @period
61
+ calendar[unit] = object = Calendar.send(:create, unit, **conf[unit])
62
+ end
63
+ [replace(calendar), object.from, object.to]
109
64
  end
110
- # :nocov:
111
65
 
112
- class << self
113
- # Create a subclass instance by unit name (internal use)
114
- def create(unit, vars)
115
- raise InternalError, "unit must be in #{UNITS.inspect}; got #{unit}" unless UNITS.include?(unit)
116
-
117
- name = unit.to_s
118
- name[0] = name[0].capitalize
119
- Object.const_get("Pagy::Calendar::#{name}").new(vars)
66
+ # Return the calendar object at time
67
+ def calendar_at(time, **opts)
68
+ conf = Marshal.load(Marshal.dump(@conf))
69
+ page_params = {}
70
+ @units.inject(nil) do |object, unit|
71
+ conf[unit][:period] = object&.send(:active_period) || @period
72
+ conf[unit][:page] = page_params[:"#{unit}_#{@page_param}"] \
73
+ = Calendar.send(:create, unit, **conf[unit]).send(:page_at, time, **opts)
74
+ conf[unit][:params] ||= {}
75
+ conf[unit][:params].merge!(page_params)
76
+ Calendar.send(:create, unit, **conf[unit])
120
77
  end
121
78
  end
122
79
  end
@@ -1,39 +1,38 @@
1
1
  # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/countless
2
2
  # frozen_string_literal: true
3
3
 
4
- require_relative '../pagy'
5
-
6
- class Pagy
4
+ class Pagy # :nodoc:
7
5
  # No need to know the count to paginate
8
6
  class Countless < Pagy
9
7
  # Merge and validate the options, do some simple arithmetic and set a few instance variables
10
- def initialize(vars = {}) # rubocop:disable Lint/MissingSuper
11
- normalize_vars(vars)
12
- setup_vars(page: 1, outset: 0)
13
- setup_items_var
14
- setup_offset_var
8
+ def initialize(**vars) # rubocop:disable Lint/MissingSuper
9
+ assign_vars(DEFAULT, vars)
10
+ assign_and_check(page: 1, outset: 0)
11
+ assign_limit
12
+ assign_offset
15
13
  end
16
14
 
17
15
  # Finalize the instance variables based on the fetched size
18
16
  def finalize(fetched_size)
19
17
  raise OverflowError.new(self, :page, "to be < #{@page}", @page) if fetched_size.zero? && @page > 1
20
18
 
21
- @last = fetched_size > @items ? @page + 1 : @page
22
- @last = vars[:max_pages] if vars[:max_pages] && @last > vars[:max_pages]
23
- raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
24
-
25
- @in = [fetched_size, @items].min
19
+ @last = fetched_size > @limit ? @page + 1 : @page
20
+ @last = @vars[:max_pages] if @vars[:max_pages] && @last > @vars[:max_pages]
21
+ check_overflow
22
+ @in = [fetched_size, @limit].min
26
23
  @from = @in.zero? ? 0 : @offset - @outset + 1
27
24
  @to = @offset - @outset + @in
28
- @prev = (@page - 1 unless @page == 1)
29
- @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
25
+ assign_prev_and_next
30
26
  self
31
27
  end
28
+ end
32
29
 
30
+ module SeriesOverride # :nodoc:
33
31
  # Override the original series.
34
32
  # Return nil if :countless_minimal is enabled
35
- def series(*, **)
33
+ def series(**)
36
34
  super unless @vars[:countless_minimal]
37
35
  end
38
36
  end
37
+ prepend SeriesOverride
39
38
  end