pagy 5.4.0 → 5.5.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.
- checksums.yaml +4 -4
- data/lib/config/pagy.rb +24 -10
- data/lib/javascripts/pagy.js +1 -1
- data/lib/pagy/calendar/day.rb +29 -0
- data/lib/pagy/calendar/month.rb +44 -0
- data/lib/pagy/calendar/week.rb +39 -0
- data/lib/pagy/calendar/year.rb +29 -0
- data/lib/pagy/calendar.rb +36 -133
- data/lib/pagy/extras/calendar.rb +26 -27
- data/lib/pagy/extras/overflow.rb +14 -14
- data/lib/pagy.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78c55595ac8caaa34c73688103333dc62e9ebb395696725e7c305e5ace3d7f42
|
4
|
+
data.tar.gz: 3e259f8f72025fc7955fb6107920be08025539d5178e5c4c714173d26aa05da1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fede5003b2a686ee972d7dd93a80b5221704518212adb8faa7b133c309b2ece0094f3fbe72575fb4e29b706a819f5dc447fc6f3a89b4c3fe202e23542dfee310
|
7
|
+
data.tar.gz: 15840445b0afa7e343c9f0f4ad196b847cb564d8d2a5da56b94a9a59ac348731fc6bb104c282343f77654a0d07090dd501957e751c843c88de208779581dbbae
|
data/lib/config/pagy.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Pagy initializer file (5.
|
3
|
+
# Pagy initializer file (5.5.0)
|
4
4
|
# Customize only what you really need and notice that Pagy works also without any of the following lines.
|
5
5
|
# Should you just cherry pick part of this file, please maintain the require-order of the extras
|
6
6
|
|
@@ -40,15 +40,30 @@
|
|
40
40
|
# See https://ddnexus.github.io/pagy/extras/array
|
41
41
|
# require 'pagy/extras/array'
|
42
42
|
|
43
|
-
# Calendar extra:
|
43
|
+
# Calendar extra: Add pagination filtering by calendar time unit (Year, Month, Week Day)
|
44
44
|
# See https://ddnexus.github.io/pagy/extras/calendar
|
45
45
|
# require 'pagy/extras/calendar'
|
46
|
-
#
|
47
|
-
# DEFAULT[:
|
48
|
-
# DEFAULT[:
|
49
|
-
#
|
50
|
-
# DEFAULT[:
|
51
|
-
# DEFAULT[:
|
46
|
+
# Default for each unit
|
47
|
+
# Pagy::Calendar::Year::DEFAULT[:order] = :asc # Time direction of pagination
|
48
|
+
# Pagy::Calendar::Year::DEFAULT[:format] = '%Y' # strftime format
|
49
|
+
#
|
50
|
+
# Pagy::Calendar::Month::DEFAULT[:order] = :asc # Time direction of pagination
|
51
|
+
# Pagy::Calendar::Month::DEFAULT[:format] = '%Y-%m' # strftime format
|
52
|
+
#
|
53
|
+
# Pagy::Calendar::Week::DEFAULT[:order] = :asc # Time direction of pagination
|
54
|
+
# Pagy::Calendar::Week::DEFAULT[:format] = '%Y-%W' # strftime format
|
55
|
+
# Pagy::Calendar::Week::DEFAULT[:offset] = 0 # Day offset from Sunday (0: Sunday; 1: Monday;... 6: Saturday)
|
56
|
+
#
|
57
|
+
# Pagy::Calendar::Day::DEFAULT[:order] = :asc # Time direction of pagination
|
58
|
+
# Pagy::Calendar::Day::DEFAULT[:format] = '%Y-%m-%d' # strftime format
|
59
|
+
#
|
60
|
+
# Uncomment the following block, if you need calendar localization without using the I18n extra
|
61
|
+
# module LocalizePagyCalendar
|
62
|
+
# def localize(time, opts)
|
63
|
+
# ::I18n.l(time, **opts)
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
# Pagy::Calendar.prepend LocalizePagyCalendar
|
52
67
|
|
53
68
|
# Countless extra: Paginate without any count, saving one query per rendering
|
54
69
|
# See https://ddnexus.github.io/pagy/extras/countless
|
@@ -217,6 +232,5 @@
|
|
217
232
|
# Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default
|
218
233
|
|
219
234
|
|
220
|
-
# When you are done setting your own
|
221
|
-
# so it will not changed accidentally
|
235
|
+
# When you are done setting your own default freeze it, so it will not get changed accidentally
|
222
236
|
Pagy::DEFAULT.freeze
|
data/lib/javascripts/pagy.js
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
# See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
class Pagy # :nodoc:
|
5
|
+
class Calendar # :nodoc:
|
6
|
+
# Calendar day subclass
|
7
|
+
class Day < Calendar
|
8
|
+
DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
|
9
|
+
format: '%Y-%m-%d' }
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
# Setup the calendar variables
|
14
|
+
def setup_unit_vars
|
15
|
+
super
|
16
|
+
@initial = new_time(@starting.year, @starting.month, @starting.day)
|
17
|
+
@final = new_time(@ending.year, @ending.month, @ending.day) + DAY
|
18
|
+
@pages = @last = (@final - @initial).to_i / DAY
|
19
|
+
@from = time_for(@page)
|
20
|
+
@to = @from + DAY
|
21
|
+
end
|
22
|
+
|
23
|
+
# Time for the page
|
24
|
+
def time_for(page)
|
25
|
+
@initial + (snap(page) * DAY)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
class Pagy # :nodoc:
|
5
|
+
class Calendar # :nodoc:
|
6
|
+
# Calendar month subclass
|
7
|
+
class Month < Calendar
|
8
|
+
DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
|
9
|
+
format: '%Y-%m' }
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
# Setup the calendar variables
|
14
|
+
def setup_unit_vars
|
15
|
+
super
|
16
|
+
@initial = new_time(@starting.year, @starting.month)
|
17
|
+
@final = bump_month(@ending)
|
18
|
+
@pages = @last = months(@final) - months(@initial)
|
19
|
+
@from = time_for(@page)
|
20
|
+
@to = bump_month(@from)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Time for the page
|
24
|
+
def time_for(page)
|
25
|
+
bump_month(@initial, snap(page))
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Months in local time
|
31
|
+
def months(time)
|
32
|
+
(time.year * 12) + time.month
|
33
|
+
end
|
34
|
+
|
35
|
+
# Add 1 or more months to local time
|
36
|
+
def bump_month(time, months = 1)
|
37
|
+
months += months(time)
|
38
|
+
year = months / 12
|
39
|
+
month = months % 12
|
40
|
+
month.zero? ? new_time(year - 1, 12) : new_time(year, month)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
class Pagy # :nodoc:
|
5
|
+
class Calendar # :nodoc:
|
6
|
+
# Calendar week subclass
|
7
|
+
class Week < Calendar
|
8
|
+
DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
|
9
|
+
format: '%Y-%W',
|
10
|
+
offset: 0 }
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
# Setup the calendar variables
|
15
|
+
def setup_unit_vars
|
16
|
+
setup_vars(offset: 0)
|
17
|
+
super
|
18
|
+
@initial = week_start(@starting)
|
19
|
+
@final = week_start(@ending) + WEEK
|
20
|
+
@pages = @last = (@final - @initial).to_i / WEEK
|
21
|
+
@from = time_for(@page)
|
22
|
+
@to = @from + WEEK
|
23
|
+
end
|
24
|
+
|
25
|
+
# Time for the page
|
26
|
+
def time_for(page)
|
27
|
+
@initial + (snap(page) * WEEK)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# Return the start of the week for local time
|
33
|
+
def week_start(time)
|
34
|
+
start = time - (((time.wday - @offset) * DAY) % WEEK)
|
35
|
+
new_time(start.year, start.month, start.day)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
class Pagy # :nodoc:
|
5
|
+
class Calendar # :nodoc:
|
6
|
+
# Calendar year subclass
|
7
|
+
class Year < Calendar
|
8
|
+
DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
|
9
|
+
format: '%Y' }
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
# Setup the calendar variables
|
14
|
+
def setup_unit_vars
|
15
|
+
super
|
16
|
+
@initial = new_time(@starting.year)
|
17
|
+
@final = new_time(@ending.year + 1)
|
18
|
+
@pages = @last = @final.year - @initial.year
|
19
|
+
@from = time_for(@page)
|
20
|
+
@to = new_time(@from.year + 1)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Time for the page
|
24
|
+
def time_for(page)
|
25
|
+
new_time(@initial.year + snap(page))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/pagy/calendar.rb
CHANGED
@@ -2,35 +2,21 @@
|
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'pagy'
|
5
|
-
require 'date'
|
6
5
|
|
7
6
|
class Pagy # :nodoc:
|
8
|
-
DEFAULT[:year_format] = '%Y' # strftime format for :year unit
|
9
|
-
DEFAULT[:month_format] = '%Y-%m' # strftime format for :month unit
|
10
|
-
DEFAULT[:week_format] = '%Y-%W' # strftime format for :week unit
|
11
|
-
DEFAULT[:day_format] = '%Y-%m-%d' # strftime format for :day unit
|
12
|
-
DEFAULT[:week_offset] = 0 # Day offset from Sunday (0: Sunday; 1: Monday;... 6: Saturday)
|
13
|
-
DEFAULT[:time_order] = :asc # Time direction of pagination
|
14
|
-
|
15
7
|
# Base class for time units subclasses (Year, Month, Week, Day)
|
16
8
|
class Calendar < Pagy
|
17
9
|
DAY = 60 * 60 * 24
|
18
10
|
WEEK = DAY * 7
|
19
11
|
|
20
|
-
attr_reader :
|
21
|
-
|
22
|
-
# Create a subclass instance
|
23
|
-
def self.create(unit, vars)
|
24
|
-
raise InternalError, "unit must be in #{UNITS.keys.inspect}; got #{unit}" unless UNITS.key?(unit)
|
25
|
-
|
26
|
-
UNITS[unit].new(vars)
|
27
|
-
end
|
12
|
+
attr_reader :order
|
28
13
|
|
29
14
|
# Merge and validate the options, do some simple arithmetic and set a few instance variables
|
30
15
|
def initialize(vars) # rubocop:disable Lint/MissingSuper
|
31
|
-
raise InternalError, 'Pagy::Calendar is a base class; use one of its subclasses
|
16
|
+
raise InternalError, 'Pagy::Calendar is a base class; use one of its subclasses' if instance_of?(Pagy::Calendar)
|
32
17
|
|
33
18
|
normalize_vars(vars)
|
19
|
+
@vars = self.class::DEFAULT.merge(@vars)
|
34
20
|
setup_vars(page: 1)
|
35
21
|
setup_unit_vars
|
36
22
|
setup_params_var
|
@@ -45,24 +31,29 @@ class Pagy # :nodoc:
|
|
45
31
|
label_for(@page, **opts)
|
46
32
|
end
|
47
33
|
|
48
|
-
#
|
49
|
-
def
|
50
|
-
[
|
34
|
+
# The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
|
35
|
+
def label_for(page, **opts)
|
36
|
+
opts[:format] ||= @vars[:format]
|
37
|
+
localize(time_for(page.to_i), **opts)
|
51
38
|
end
|
52
39
|
|
53
|
-
|
40
|
+
# Period of the active page (used for nested units)
|
41
|
+
def active_period
|
42
|
+
[[@starting, @from].max, [@to - DAY, @ending].min] # include only last unit day
|
43
|
+
end
|
54
44
|
|
55
|
-
|
56
|
-
raise VariableError.new(self, format, 'to be a strftime format', @vars[format]) unless @vars[format].is_a?(String)
|
57
|
-
raise VariableError.new(self, :time_order, 'to be in [:asc, :desc]', @time_order) \
|
58
|
-
unless %i[asc desc].include?(@time_order = @vars[:time_order])
|
45
|
+
protected
|
59
46
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
47
|
+
# Base class method for the setup of the unit variables
|
48
|
+
def setup_unit_vars
|
49
|
+
raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
|
50
|
+
raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
|
51
|
+
unless %i[asc desc].include?(@order = @vars[:order])
|
64
52
|
|
65
|
-
|
53
|
+
@starting, @ending = @vars[:period]
|
54
|
+
raise VariableError.new(self, :period, 'to be a an Array of min and max local Time instances', @vars[:period]) \
|
55
|
+
unless @starting.is_a?(Time) && @ending.is_a?(Time) && !@starting.utc? && !@ending.utc? && @starting <= @ending \
|
56
|
+
&& (@utc_offset = @starting.utc_offset) == @ending.utc_offset
|
66
57
|
end
|
67
58
|
|
68
59
|
# Apply the strftime format to the time (overridden by the i18n extra when localization is required)
|
@@ -70,118 +61,30 @@ class Pagy # :nodoc:
|
|
70
61
|
time.strftime(opts[:format])
|
71
62
|
end
|
72
63
|
|
73
|
-
# Simple trick to snap the page into its ordered position, without actually reordering anything in the internal structure
|
74
|
-
|
75
|
-
|
64
|
+
# Simple trick to snap the page into its ordered position, without actually reordering anything in the internal structure.
|
65
|
+
# Tech note: use for @from but not for @to, which must be one period after @from also in :desc order!
|
66
|
+
def snap(page)
|
67
|
+
@order == :asc ? page - 1 : @pages - page
|
76
68
|
end
|
77
69
|
|
78
70
|
# Create a new local time at the beginning of the day
|
79
71
|
def new_time(year, month = 1, day = 1)
|
80
72
|
Time.new(year, month, day, 0, 0, 0, @utc_offset)
|
81
73
|
end
|
74
|
+
end
|
75
|
+
require 'pagy/calendar/year'
|
76
|
+
require 'pagy/calendar/month'
|
77
|
+
require 'pagy/calendar/week'
|
78
|
+
require 'pagy/calendar/day'
|
82
79
|
|
83
|
-
|
84
|
-
|
85
|
-
# Setup the calendar vars when the unit is :year
|
86
|
-
def setup_unit_vars
|
87
|
-
min, max = super(:year_format)
|
88
|
-
@initial = new_time(min.year)
|
89
|
-
@final = new_time(max.year + 1)
|
90
|
-
@pages = @last = @final.year - @initial.year
|
91
|
-
@utc_from = new_time(@initial.year + snap).utc
|
92
|
-
@utc_to = new_time(@initial.year + snap + 1).utc
|
93
|
-
end
|
94
|
-
|
95
|
-
# Generate a label for each page (it can pass along the I18n gem opts when it's used with the i18n extra)
|
96
|
-
def label_for(page, **opts)
|
97
|
-
opts[:format] ||= @vars[:year_format]
|
98
|
-
localize(new_time(@initial.year + snap(page.to_i)), **opts)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# Calendar month subclass
|
103
|
-
class Month < Calendar
|
104
|
-
# Setup the calendar vars when the unit is :month
|
105
|
-
def setup_unit_vars
|
106
|
-
min, max = super(:month_format)
|
107
|
-
@initial = new_time(min.year, min.month)
|
108
|
-
@final = bump_month(max)
|
109
|
-
@pages = @last = months(@final) - months(@initial)
|
110
|
-
@utc_from = bump_month(@initial, snap).utc
|
111
|
-
@utc_to = bump_month(@initial, snap + 1).utc
|
112
|
-
end
|
113
|
-
|
114
|
-
# Generate a label for each page (it can pass along the I18n gem opts when it's used with the i18n extra)
|
115
|
-
def label_for(page, **opts)
|
116
|
-
opts[:format] ||= @vars[:month_format]
|
117
|
-
localize(bump_month(@initial, snap(page.to_i)), **opts)
|
118
|
-
end
|
119
|
-
|
120
|
-
private
|
121
|
-
|
122
|
-
# Months in local time
|
123
|
-
def months(time)
|
124
|
-
(time.year * 12) + time.month
|
125
|
-
end
|
126
|
-
|
127
|
-
# Add 1 or more months to local time
|
128
|
-
def bump_month(time, months = 1)
|
129
|
-
months += months(time)
|
130
|
-
year = months / 12
|
131
|
-
month = months % 12
|
132
|
-
month.zero? ? new_time(year - 1, 12) : new_time(year, month)
|
133
|
-
end
|
134
|
-
end
|
80
|
+
class Calendar # :nodoc:
|
81
|
+
UNITS = { year: Year, month: Month, week: Week, day: Day }.freeze
|
135
82
|
|
136
|
-
#
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
# Setup the calendar vars when the unit is :week
|
141
|
-
def setup_unit_vars
|
142
|
-
setup_vars(week_offset: 0)
|
143
|
-
min, max = super(:week_format)
|
144
|
-
@initial = week_start(min)
|
145
|
-
@final = week_start(max) + WEEK
|
146
|
-
@pages = @last = (@final - @initial).to_i / WEEK
|
147
|
-
@utc_from = (@initial + (snap * WEEK)).utc
|
148
|
-
@utc_to = @utc_from + WEEK
|
149
|
-
end
|
150
|
-
|
151
|
-
# Generate a label for each page (it can pass along the I18n gem opts when it's used with the i18n extra)
|
152
|
-
def label_for(page, **opts)
|
153
|
-
opts[:format] ||= @vars[:week_format]
|
154
|
-
localize(@initial + (snap(page.to_i) * WEEK), **opts)
|
155
|
-
end
|
156
|
-
|
157
|
-
private
|
158
|
-
|
159
|
-
# Return the start of the week for local time
|
160
|
-
def week_start(time)
|
161
|
-
start = time - (((time.wday - @week_offset) * DAY) % WEEK)
|
162
|
-
new_time(start.year, start.month, start.day)
|
163
|
-
end
|
164
|
-
end
|
83
|
+
# Create a subclass instance by unit name (internal use)
|
84
|
+
def self.create(unit, vars)
|
85
|
+
raise InternalError, "unit must be in #{UNITS.keys.inspect}; got #{unit}" unless UNITS.key?(unit)
|
165
86
|
|
166
|
-
|
167
|
-
class Day < Calendar
|
168
|
-
# Setup the calendar vars when the unit is :day
|
169
|
-
def setup_unit_vars
|
170
|
-
min, max = super(:day_format)
|
171
|
-
@initial = new_time(min.year, min.month, min.day)
|
172
|
-
@final = new_time(max.year, max.month, max.day) + DAY
|
173
|
-
@pages = @last = (@final - @initial).to_i / DAY
|
174
|
-
@utc_from = (@initial + (snap * DAY)).utc
|
175
|
-
@utc_to = @utc_from + DAY
|
176
|
-
end
|
177
|
-
|
178
|
-
# Generate a label for each page (it can pass along the I18n gem opts when it's used with the i18n extra)
|
179
|
-
def label_for(page, **opts)
|
180
|
-
opts[:format] ||= @vars[:day_format]
|
181
|
-
localize(@initial + (snap(page.to_i) * DAY), **opts)
|
182
|
-
end
|
87
|
+
UNITS[unit].new(vars)
|
183
88
|
end
|
184
|
-
# After all the subclasses are defined
|
185
|
-
UNITS = { year: Year, month: Month, week: Week, day: Day }.freeze
|
186
89
|
end
|
187
90
|
end
|
data/lib/pagy/extras/calendar.rb
CHANGED
@@ -8,68 +8,67 @@ class Pagy # :nodoc:
|
|
8
8
|
module CalendarExtra
|
9
9
|
# Additions for the Backend module
|
10
10
|
module Backend
|
11
|
-
CONF_KEYS = %i[year month week day pagy
|
11
|
+
CONF_KEYS = %i[year month week day pagy active].freeze
|
12
12
|
|
13
13
|
private
|
14
14
|
|
15
|
-
# Take a collection and a conf Hash with keys in [:year, :month: week, :day, :pagy]
|
15
|
+
# Take a collection and a conf Hash with keys in [:year, :month: week, :day, :pagy: :active];
|
16
|
+
# The calendar is active by default, but it can be explicitly inactivated with `active: false`
|
16
17
|
# Return a hash with 3 items:
|
17
18
|
# 0. Array of pagy calendar unit objects
|
18
19
|
# 1. Pagy object
|
19
20
|
# 2. Array of results
|
20
21
|
def pagy_calendar(collection, conf)
|
21
|
-
unless conf.is_a?(Hash) && (conf.keys - CONF_KEYS).empty? && conf.all? { |k, v| v.is_a?(Hash) || k == :
|
22
|
+
unless conf.is_a?(Hash) && (conf.keys - CONF_KEYS).empty? && conf.all? { |k, v| v.is_a?(Hash) || k == :active }
|
22
23
|
raise ArgumentError, "keys must be in #{CONF_KEYS.inspect} and object values must be Hashes; got #{conf.inspect}"
|
23
24
|
end
|
24
25
|
|
25
26
|
conf[:pagy] = {} unless conf[:pagy] # use default Pagy object when omitted
|
26
|
-
calendar, collection = pagy_setup_calendar(collection, conf) unless conf[:
|
27
|
+
calendar, collection = pagy_setup_calendar(collection, conf) unless conf.key?(:active) && !conf[:active]
|
27
28
|
pagy, result = send(conf[:pagy][:backend] || :pagy, collection, conf[:pagy]) # use backend: :pagy when omitted
|
28
29
|
[calendar, pagy, result]
|
29
30
|
end
|
30
31
|
|
31
|
-
# Setup the calendar objects and
|
32
|
+
# Setup and return the calendar objects and the filtered collection
|
32
33
|
def pagy_setup_calendar(collection, conf)
|
33
|
-
units
|
34
|
+
units = Calendar::UNITS.keys & conf.keys
|
34
35
|
page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
|
35
36
|
units.each do |unit| # set all the :page_param vars for later deletion
|
36
37
|
unit_page_param = :"#{unit}_#{page_param}"
|
37
38
|
conf[unit][:page_param] = unit_page_param
|
38
39
|
conf[unit][:page] = params[unit_page_param]
|
39
40
|
end
|
40
|
-
calendar
|
41
|
-
|
42
|
-
last_minmax = minmax = pagy_calendar_minmax(collection)
|
41
|
+
calendar = {}
|
42
|
+
period = pagy_calendar_period(collection)
|
43
43
|
units.each_with_index do |unit, index|
|
44
44
|
params_to_delete = units[(index + 1), units.size].map { |sub| conf[sub][:page_param] } + [page_param]
|
45
45
|
conf[unit][:params] = lambda do |params| # delete page_param from the sub-units
|
46
|
-
params_to_delete.each { |p| params.delete(p.to_s) } # except
|
46
|
+
params_to_delete.each { |p| params.delete(p.to_s) } # Hash#except missing from 2.5 baseline
|
47
47
|
params
|
48
48
|
end
|
49
|
-
conf[unit][:
|
50
|
-
calendar[unit] =
|
51
|
-
|
49
|
+
conf[unit][:period] = period
|
50
|
+
calendar[unit] = Calendar.create(unit, conf[unit])
|
51
|
+
period = calendar[unit].active_period # set the period for the next unit
|
52
52
|
end
|
53
|
-
|
54
|
-
[calendar, filtered]
|
53
|
+
[calendar, pagy_calendar_filter(collection, calendar[units.last].from, calendar[units.last].to)]
|
55
54
|
end
|
56
55
|
|
57
56
|
# This method must be implemented by the application.
|
58
|
-
# It must return
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
'an Array with the minimum and maximum local Time objects of the collection'
|
57
|
+
# It must return the the starting and ending local Time objects defining the calendar :period
|
58
|
+
def pagy_calendar_period(*)
|
59
|
+
# return_period_array_using(collection)
|
60
|
+
raise NoMethodError, 'the pagy_calendar_period method must be implemented by the application and must return ' \
|
61
|
+
'the starting and ending local Time objects array defining the calendar :period'
|
64
62
|
end
|
65
63
|
|
66
64
|
# This method must be implemented by the application.
|
67
|
-
# It receives the main collection
|
68
|
-
# The filter logic must be equivalent to {
|
69
|
-
def
|
70
|
-
# collection
|
71
|
-
raise NoMethodError, 'the
|
72
|
-
'collection filtered by a logic equivalent to
|
65
|
+
# It receives the main collection and must return a filtered version of it.
|
66
|
+
# The filter logic must be equivalent to {storage_time >= from && storage_time < to}
|
67
|
+
def pagy_calendar_filter(*)
|
68
|
+
# return_filtered_collection_using(collection, from, to)
|
69
|
+
raise NoMethodError, 'the pagy_calendar_filter method must be implemented by the application and must return the ' \
|
70
|
+
'collection filtered by a logic equivalent to '\
|
71
|
+
'{storage_time >= from && storage_time < to}'
|
73
72
|
end
|
74
73
|
end
|
75
74
|
end
|
data/lib/pagy/extras/overflow.rb
CHANGED
@@ -15,25 +15,25 @@ class Pagy # :nodoc:
|
|
15
15
|
|
16
16
|
# Add rescue clause for different behaviors
|
17
17
|
def initialize(vars)
|
18
|
-
@overflow ||= false
|
18
|
+
@overflow ||= false # still true if :last_page re-run the method after an overflow
|
19
19
|
super
|
20
20
|
rescue OverflowError
|
21
|
-
@overflow = true
|
21
|
+
@overflow = true # add the overflow flag
|
22
22
|
case @vars[:overflow]
|
23
23
|
when :exception
|
24
|
-
raise
|
24
|
+
raise # same as without the extra
|
25
25
|
when :last_page
|
26
|
-
requested_page = @vars[:page]
|
27
|
-
initialize vars.merge!(page: @last)
|
28
|
-
@vars[:page] = requested_page
|
26
|
+
requested_page = @vars[:page] # save the requested page (even after re-run)
|
27
|
+
initialize vars.merge!(page: @last) # re-run with the last page
|
28
|
+
@vars[:page] = requested_page # restore the requested page
|
29
29
|
when :empty_page
|
30
|
-
@offset = @items = @from = @to = 0
|
31
|
-
if defined?(Calendar) && is_a?(Calendar)
|
32
|
-
edge = @
|
33
|
-
@
|
30
|
+
@offset = @items = @from = @to = 0 # vars relative to the actual page
|
31
|
+
if defined?(Calendar) && is_a?(Calendar) # only for Calendar instances
|
32
|
+
edge = @order == :asc ? @final : @initial # get the edge of the overflow side (neat, but any time would do)
|
33
|
+
@from = @to = edge # set both to the edge utc time (a >=&&< query will get no records)
|
34
34
|
end
|
35
|
-
@prev = @last
|
36
|
-
extend Series
|
35
|
+
@prev = @last # prev relative to the actual page
|
36
|
+
extend Series # special series for :empty_page
|
37
37
|
else
|
38
38
|
raise VariableError.new(self, :overflow, 'to be in [:last_page, :empty_page, :exception]', @vars[:overflow])
|
39
39
|
end
|
@@ -43,7 +43,7 @@ class Pagy # :nodoc:
|
|
43
43
|
module Series
|
44
44
|
def series(*)
|
45
45
|
@page = @last # series for last page
|
46
|
-
super.tap do |s|
|
46
|
+
super.tap do |s| # call original series
|
47
47
|
s[s.index(@page.to_s)] = @page # string to integer (i.e. no current page)
|
48
48
|
@page = @vars[:page] # restore the actual page
|
49
49
|
end
|
@@ -63,7 +63,7 @@ class Pagy # :nodoc:
|
|
63
63
|
when :exception
|
64
64
|
raise # same as without the extra
|
65
65
|
when :empty_page
|
66
|
-
@offset = @items = @from = @to = 0
|
66
|
+
@offset = @items = @from = @to = 0 # vars relative to the actual page
|
67
67
|
@vars[:size] = [] # no page in the series
|
68
68
|
self
|
69
69
|
else
|
data/lib/pagy.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pagy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Domizio Demichelis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-11-
|
11
|
+
date: 2021-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Agnostic pagination in plain ruby. It does it all. Better.
|
14
14
|
email:
|
@@ -54,6 +54,10 @@ files:
|
|
54
54
|
- lib/pagy.rb
|
55
55
|
- lib/pagy/backend.rb
|
56
56
|
- lib/pagy/calendar.rb
|
57
|
+
- lib/pagy/calendar/day.rb
|
58
|
+
- lib/pagy/calendar/month.rb
|
59
|
+
- lib/pagy/calendar/week.rb
|
60
|
+
- lib/pagy/calendar/year.rb
|
57
61
|
- lib/pagy/console.rb
|
58
62
|
- lib/pagy/countless.rb
|
59
63
|
- lib/pagy/exceptions.rb
|