pagy 7.0.11 → 8.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/apps/calendar.ru +745 -0
  3. data/apps/demo.ru +435 -0
  4. data/apps/rails.ru +212 -0
  5. data/apps/repro.ru +177 -0
  6. data/apps/tmp/calendar.sqlite3 +0 -0
  7. data/apps/tmp/calendar.sqlite3-shm +0 -0
  8. data/apps/tmp/calendar.sqlite3-wal +0 -0
  9. data/apps/tmp/local_secret.txt +1 -0
  10. data/apps/tmp/pagy-keyset.sqlite3 +0 -0
  11. data/apps/tmp/pagy-keyset.sqlite3-shm +0 -0
  12. data/apps/tmp/pagy-keyset.sqlite3-wal +0 -0
  13. data/bin/pagy +100 -0
  14. data/{lib/config → config}/pagy.rb +31 -73
  15. data/javascripts/pagy-module.js +100 -0
  16. data/javascripts/pagy.js +4 -0
  17. data/javascripts/pagy.min.js +4 -0
  18. data/javascripts/pagy.min.js.map +10 -0
  19. data/javascripts/pagy.mjs +100 -0
  20. data/lib/optimist.rb +1022 -0
  21. data/lib/pagy/backend.rb +8 -3
  22. data/lib/pagy/calendar/day.rb +4 -3
  23. data/lib/pagy/calendar/month.rb +4 -3
  24. data/lib/pagy/calendar/quarter.rb +4 -3
  25. data/lib/pagy/calendar/unit.rb +106 -0
  26. data/lib/pagy/calendar/week.rb +3 -3
  27. data/lib/pagy/calendar/year.rb +4 -3
  28. data/lib/pagy/calendar.rb +55 -99
  29. data/lib/pagy/console.rb +2 -2
  30. data/lib/pagy/countless.rb +15 -10
  31. data/lib/pagy/extras/arel.rb +1 -1
  32. data/lib/pagy/extras/array.rb +1 -1
  33. data/lib/pagy/extras/bootstrap.rb +52 -63
  34. data/lib/pagy/extras/bulma.rb +49 -64
  35. data/lib/pagy/extras/calendar.rb +35 -5
  36. data/lib/pagy/extras/countless.rb +2 -2
  37. data/lib/pagy/extras/foundation.rb +52 -62
  38. data/lib/pagy/extras/gearbox.rb +28 -27
  39. data/lib/pagy/extras/headers.rb +1 -1
  40. data/lib/pagy/extras/i18n.rb +1 -1
  41. data/lib/pagy/extras/items.rb +21 -18
  42. data/lib/pagy/extras/{frontend_helpers.rb → js_tools.rb} +9 -6
  43. data/lib/pagy/extras/jsonapi.rb +2 -2
  44. data/lib/pagy/extras/materialize.rb +56 -52
  45. data/lib/pagy/extras/metadata.rb +6 -2
  46. data/lib/pagy/extras/overflow.rb +5 -4
  47. data/lib/pagy/extras/pagy.rb +82 -0
  48. data/lib/pagy/extras/semantic.rb +50 -51
  49. data/lib/pagy/extras/size.rb +40 -0
  50. data/lib/pagy/extras/standalone.rb +2 -2
  51. data/lib/pagy/extras/trim.rb +12 -12
  52. data/lib/pagy/extras/uikit.rb +51 -50
  53. data/lib/pagy/frontend.rb +39 -53
  54. data/lib/pagy/url_helpers.rb +9 -10
  55. data/lib/pagy.rb +51 -82
  56. data/{lib/locales → locales}/ar.yml +10 -11
  57. data/{lib/locales → locales}/be.yml +5 -5
  58. data/{lib/locales → locales}/bg.yml +5 -5
  59. data/{lib/locales → locales}/bs.yml +5 -5
  60. data/locales/ca.yml +21 -0
  61. data/locales/ckb.yml +18 -0
  62. data/{lib/locales → locales}/cs.yml +5 -5
  63. data/locales/da.yml +21 -0
  64. data/{lib/locales → locales}/de.yml +5 -5
  65. data/{lib/locales → locales}/en.yml +5 -5
  66. data/{lib/locales → locales}/es.yml +3 -3
  67. data/{lib/locales → locales}/fr.yml +5 -5
  68. data/{lib/locales → locales}/hr.yml +5 -5
  69. data/{lib/locales → locales}/id.yml +5 -5
  70. data/{lib/locales → locales}/it.yml +5 -5
  71. data/{lib/locales → locales}/ja.yml +5 -5
  72. data/{lib/locales → locales}/km.yml +5 -5
  73. data/locales/ko.yml +17 -0
  74. data/{lib/locales → locales}/nb.yml +5 -5
  75. data/{lib/locales → locales}/nl.yml +5 -5
  76. data/{lib/locales → locales}/nn.yml +5 -5
  77. data/{lib/locales → locales}/pl.yml +5 -5
  78. data/{lib/locales → locales}/pt-BR.yml +3 -3
  79. data/{lib/locales → locales}/pt.yml +3 -3
  80. data/locales/ru.yml +25 -0
  81. data/{lib/locales → locales}/sr.yml +5 -5
  82. data/{lib/locales → locales}/sv-SE.yml +5 -5
  83. data/{lib/locales → locales}/sv.yml +5 -5
  84. data/{lib/locales → locales}/sw.yml +5 -5
  85. data/{lib/locales → locales}/ta.yml +5 -5
  86. data/{lib/locales → locales}/tr.yml +5 -5
  87. data/{lib/locales → locales}/uk.yml +5 -5
  88. data/locales/vi.yml +17 -0
  89. data/{lib/locales → locales}/zh-CN.yml +5 -5
  90. data/{lib/locales → locales}/zh-HK.yml +5 -5
  91. data/{lib/locales → locales}/zh-TW.yml +5 -5
  92. data/{lib/stylesheets → stylesheets}/pagy.css +19 -34
  93. data/{lib/stylesheets → stylesheets}/pagy.scss +17 -19
  94. data/stylesheets/pagy.tailwind.css +21 -0
  95. metadata +76 -53
  96. data/lib/javascripts/pagy-dev.js +0 -112
  97. data/lib/javascripts/pagy-module.js +0 -111
  98. data/lib/javascripts/pagy.js +0 -1
  99. data/lib/locales/ca.yml +0 -23
  100. data/lib/locales/ckb.yml +0 -18
  101. data/lib/locales/da.yml +0 -23
  102. data/lib/locales/ko.yml +0 -19
  103. data/lib/locales/ru.yml +0 -27
  104. data/lib/locales/vi.yml +0 -17
  105. data/lib/pagy/calendar/helper.rb +0 -65
  106. data/lib/pagy/extras/navs.rb +0 -51
  107. data/lib/pagy/extras/support.rb +0 -40
  108. data/lib/stylesheets/pagy.tailwind.scss +0 -24
  109. /data/{lib/javascripts/pagy-module.d.ts → javascripts/pagy.d.ts} +0 -0
data/lib/pagy/backend.rb CHANGED
@@ -18,16 +18,21 @@ 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
- count_args = vars[:count_args] || DEFAULT[:count_args]
22
- vars[:count] ||= (count = collection.count(*count_args)).is_a?(Hash) ? count.size : count
21
+ vars[:count] ||= pagy_get_count(collection, vars)
23
22
  vars[:page] ||= pagy_get_page(vars)
24
23
  vars
25
24
  end
26
25
 
26
+ # Get the count from the collection
27
+ def pagy_get_count(collection, vars)
28
+ count_args = vars[:count_args] || DEFAULT[:count_args]
29
+ (count = collection.count(*count_args)).is_a?(Hash) ? count.size : count
30
+ end
31
+
27
32
  # Get the page integer from the params
28
33
  # Overridable by the jsonapi extra
29
34
  def pagy_get_page(vars)
30
- (params[vars[:page_param] || DEFAULT[:page_param]] || 1).to_i
35
+ [params[vars[:page_param] || DEFAULT[:page_param]].to_i, 1].max
31
36
  end
32
37
 
33
38
  # Sub-method called only by #pagy: here for easy customization of record-extraction by overriding
@@ -3,9 +3,10 @@
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
 
@@ -16,7 +17,7 @@ class Pagy # :nodoc:
16
17
  super
17
18
  @initial = @starting.beginning_of_day
18
19
  @final = @ending.tomorrow.beginning_of_day
19
- @pages = @last = page_offset(@initial, @final)
20
+ @last = page_offset(@initial, @final)
20
21
  @from = starting_time_for(@page)
21
22
  @to = @from.tomorrow
22
23
  end
@@ -3,9 +3,10 @@
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
 
@@ -16,7 +17,7 @@ class Pagy # :nodoc:
16
17
  super
17
18
  @initial = @starting.beginning_of_month
18
19
  @final = @ending.next_month.beginning_of_month
19
- @pages = @last = (months_in(@final) - months_in(@initial))
20
+ @last = (months_in(@final) - months_in(@initial))
20
21
  @from = starting_time_for(@page)
21
22
  @to = @from.next_month
22
23
  end
@@ -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
 
@@ -23,7 +24,7 @@ class Pagy # :nodoc:
23
24
  super
24
25
  @initial = @starting.beginning_of_quarter
25
26
  @final = @ending.next_quarter.beginning_of_quarter
26
- @pages = @last = (months_in(@final) - months_in(@initial)) / 3
27
+ @last = (months_in(@final) - months_in(@initial)) / 3
27
28
  @from = starting_time_for(@page)
28
29
  @to = @from.next_quarter
29
30
  end
@@ -0,0 +1,106 @@
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
+ vars = self.class::DEFAULT.merge(vars) # subclass specific default
21
+ normalize_vars(vars) # general default
22
+ setup_vars(page: 1)
23
+ setup_unit_vars
24
+ raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
25
+
26
+ @prev = (@page - 1 unless @page == 1)
27
+ @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
28
+ end
29
+
30
+ # The label for the current page (it can pass along the I18n gem opts when it's used with the i18n extra)
31
+ def label(opts = {})
32
+ label_for(@page, opts)
33
+ end
34
+
35
+ # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
36
+ def label_for(page, opts = {})
37
+ opts[:format] ||= @vars[:format]
38
+ localize(starting_time_for(page.to_i), opts) # page could be a string
39
+ end
40
+
41
+ protected
42
+
43
+ # The page that includes time
44
+ # In case of out of range time, the :fit_time option avoids the outOfRangeError
45
+ # and returns the closest page to the passed time argument (first or last page)
46
+ def page_at(time, **opts)
47
+ fit_time = time
48
+ fit_final = @final - 1
49
+ unless time.between?(@initial, fit_final)
50
+ raise OutOfRangeError.new(self, :time, "between #{@initial} and #{fit_final}", time) unless opts[:fit_time]
51
+
52
+ if time < @final
53
+ fit_time = @initial
54
+ ordinal = 'first'
55
+ else
56
+ fit_time = fit_final
57
+ ordinal = 'last'
58
+ end
59
+ warn "Pagy::Calendar#page_at: Rescued #{time} out of range by returning the #{ordinal} page."
60
+ end
61
+ offset = page_offset_at(fit_time) # offset starts from 0
62
+ @order == :asc ? offset + 1 : @last - offset
63
+ end
64
+
65
+ # Base class method for the setup of the unit variables (subclasses must implement it and call super)
66
+ def setup_unit_vars
67
+ raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
68
+ raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
69
+ unless %i[asc desc].include?(@order = @vars[:order])
70
+
71
+ @starting, @ending = @vars[:period]
72
+ raise VariableError.new(self, :period, 'to be a an Array of min and max TimeWithZone instances', @vars[:period]) \
73
+ unless @starting.is_a?(ActiveSupport::TimeWithZone) \
74
+ && @ending.is_a?(ActiveSupport::TimeWithZone) && @starting <= @ending
75
+ end
76
+
77
+ # Apply the strftime format to the time (overridden by the i18n extra when localization is required)
78
+ def localize(time, opts)
79
+ time.strftime(opts[:format])
80
+ end
81
+
82
+ # Number of time units to offset from the @initial time, in order to get the ordered starting time for the page.
83
+ # Used in starting_time_for(page) where page starts from 1 (e.g. page to starting_time means subtracting 1)
84
+ def time_offset_for(page)
85
+ @order == :asc ? page - 1 : @last - page
86
+ end
87
+
88
+ # Period of the active page (used internally for nested units)
89
+ def active_period
90
+ [[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
91
+ end
92
+
93
+ # :nocov:
94
+ # This method must be implemented by the unit subclass
95
+ def starting_time_for(*)
96
+ raise NoMethodError, 'the starting_time_for method must be implemented by the unit subclass'
97
+ end
98
+
99
+ # This method must be implemented by the unit subclass
100
+ def page_offset_at(*)
101
+ raise NoMethodError, 'the page_offset_at method must be implemented by the unit subclass'
102
+ end
103
+ # :nocov:
104
+ end
105
+ end
106
+ end
@@ -3,8 +3,8 @@
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
 
@@ -15,7 +15,7 @@ class Pagy # :nodoc:
15
15
  super
16
16
  @initial = @starting.beginning_of_week
17
17
  @final = @ending.next_week.beginning_of_week
18
- @pages = @last = page_offset(@initial, @final)
18
+ @last = page_offset(@initial, @final)
19
19
  @from = starting_time_for(@page)
20
20
  @to = @from.next_week
21
21
  end
@@ -3,9 +3,10 @@
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
 
@@ -16,7 +17,7 @@ class Pagy # :nodoc:
16
17
  super
17
18
  @initial = @starting.beginning_of_year
18
19
  @final = @ending.next_year.beginning_of_year
19
- @pages = @last = @final.year - @initial.year
20
+ @last = @final.year - @initial.year
20
21
  @from = starting_time_for(@page)
21
22
  @to = @from.next_year
22
23
  end
data/lib/pagy/calendar.rb CHANGED
@@ -1,123 +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
- require 'pagy'
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
- setup_params_var
32
- raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
33
-
34
- @prev = (@page - 1 unless @page == 1)
35
- @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
36
- end
37
-
38
- # The label for the current page (it can pass along the I18n gem opts when it's used with the i18n extra)
39
- def label(opts = {})
40
- label_for(@page, opts)
41
- end
42
-
43
- # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
44
- def label_for(page, opts = {})
45
- opts[:format] ||= @vars[:format]
46
- localize(starting_time_for(page.to_i), opts) # page could be a string
47
- end
48
-
49
- protected
16
+ class << self
17
+ private
50
18
 
51
- # The page that includes time
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]
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)
59
22
 
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."
23
+ name = unit.to_s
24
+ name[0] = name[0].capitalize
25
+ Object.const_get("Pagy::Calendar::#{name}").new(vars)
68
26
  end
69
- offset = page_offset_at(fit_time) # offset starts from 0
70
- @order == :asc ? offset + 1 : @pages - offset
71
- end
72
27
 
73
- # Base class method for the setup of the unit variables (subclasses must implement it and call super)
74
- def setup_unit_vars
75
- raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
76
- raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
77
- unless %i[asc desc].include?(@order = @vars[:order])
78
-
79
- @starting, @ending = @vars[:period]
80
- raise VariableError.new(self, :period, 'to be a an Array of min and max TimeWithZone instances', @vars[:period]) \
81
- unless @starting.is_a?(ActiveSupport::TimeWithZone) \
82
- && @ending.is_a?(ActiveSupport::TimeWithZone) && @starting <= @ending
83
- end
84
-
85
- # Apply the strftime format to the time (overridden by the i18n extra when localization is required)
86
- def localize(time, opts)
87
- time.strftime(opts[:format])
88
- end
89
-
90
- # Number of time units to offset from the @initial time, in order to get the ordered starting time for the page.
91
- # Used in starting_time_for(page) where page starts from 1 (e.g. page to starting_time means subtracting 1)
92
- def time_offset_for(page)
93
- @order == :asc ? page - 1 : @pages - page
94
- end
95
-
96
- # Period of the active page (used internally for nested units)
97
- def active_period
98
- [[@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
99
32
  end
100
33
 
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'
34
+ # Return the current time of the smallest time unit shown
35
+ def showtime
36
+ self[@units.last].from
105
37
  end
106
38
 
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'
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]
110
64
  end
111
- # :nocov:
112
-
113
- class << self
114
- # Create a subclass instance by unit name (internal use)
115
- def create(unit, vars)
116
- raise InternalError, "unit must be in #{UNITS.inspect}; got #{unit}" unless UNITS.include?(unit)
117
65
 
118
- name = unit.to_s
119
- name[0] = name[0].capitalize
120
- 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])
121
77
  end
122
78
  end
123
79
  end
data/lib/pagy/console.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # See Pagy::Console API documentation: https://ddnexus.github.io/pagy/docs/api/console
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pagy' # so you can require just the extra in the console
5
- require 'pagy/extras/standalone'
4
+ require_relative '../pagy' # so you can require just the extra in the console
5
+ require_relative 'extras/standalone'
6
6
 
7
7
  class Pagy
8
8
  # Provide a ready to use pagy environment when included in irb/rails console
@@ -1,9 +1,9 @@
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 'pagy'
4
+ require_relative '../pagy'
5
5
 
6
- class Pagy
6
+ class Pagy # :nodoc:
7
7
  # No need to know the count to paginate
8
8
  class Countless < Pagy
9
9
  # Merge and validate the options, do some simple arithmetic and set a few instance variables
@@ -12,26 +12,31 @@ class Pagy
12
12
  setup_vars(page: 1, outset: 0)
13
13
  setup_items_var
14
14
  setup_offset_var
15
- setup_params_var
16
15
  end
17
16
 
18
17
  # Finalize the instance variables based on the fetched size
19
18
  def finalize(fetched_size)
20
19
  raise OverflowError.new(self, :page, "to be < #{@page}", @page) if fetched_size.zero? && @page > 1
21
20
 
22
- @pages = @last = (fetched_size > @items ? @page + 1 : @page)
23
- @in = [fetched_size, @items].min
24
- @from = @in.zero? ? 0 : @offset - @outset + 1
25
- @to = @offset - @outset + @in
26
- @prev = (@page - 1 unless @page == 1)
27
- @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
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
26
+ @from = @in.zero? ? 0 : @offset - @outset + 1
27
+ @to = @offset - @outset + @in
28
+ @prev = (@page - 1 unless @page == 1)
29
+ @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
28
30
  self
29
31
  end
32
+ end
30
33
 
34
+ module SeriesOverride # :nodoc:
31
35
  # Override the original series.
32
36
  # Return nil if :countless_minimal is enabled
33
- def series(*, **)
37
+ def series(**)
34
38
  super unless @vars[:countless_minimal]
35
39
  end
36
40
  end
41
+ prepend SeriesOverride
37
42
  end
@@ -16,7 +16,7 @@ class Pagy # :nodoc:
16
16
  def pagy_arel_get_vars(collection, vars)
17
17
  pagy_set_items_from_params(vars) if defined?(ItemsExtra)
18
18
  vars[:count] ||= pagy_arel_count(collection)
19
- vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
19
+ vars[:page] ||= pagy_get_page(vars)
20
20
  vars
21
21
  end
22
22
 
@@ -16,7 +16,7 @@ class Pagy # :nodoc:
16
16
  def pagy_array_get_vars(array, vars)
17
17
  pagy_set_items_from_params(vars) if defined?(ItemsExtra)
18
18
  vars[:count] ||= array.size
19
- vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
19
+ vars[:page] ||= pagy_get_page(vars)
20
20
  vars
21
21
  end
22
22
  end