pagy 5.6.2 → 5.6.6
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 +1 -1
- data/lib/javascripts/pagy.js +3 -2
- data/lib/pagy/backend.rb +1 -1
- data/lib/pagy/calendar/day.rb +1 -1
- data/lib/pagy/calendar/month_mixin.rb +10 -8
- data/lib/pagy/calendar/week.rb +7 -7
- data/lib/pagy/calendar/year.rb +1 -1
- data/lib/pagy/calendar.rb +10 -9
- data/lib/pagy/extras/arel.rb +3 -0
- data/lib/pagy/extras/array.rb +1 -1
- data/lib/pagy/extras/calendar.rb +19 -29
- data/lib/pagy/extras/frontend_helpers.rb +1 -1
- data/lib/pagy/extras/gearbox.rb +5 -5
- data/lib/pagy/extras/i18n.rb +2 -2
- data/lib/pagy/frontend.rb +1 -1
- data/lib/pagy/i18n.rb +2 -1
- data/lib/pagy.rb +5 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39e1594de6e9b2ca3919cf666e3b7735c96656447d78c39ce0a4aaf85a737487
|
4
|
+
data.tar.gz: ec8e032be1fe73b5890c1fc42c05d81c397015b468120a8cd97dcdd2bf88fcfa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3df6ce4c03f578555b88c34691be3c667c1aaf2d4712382ebb1ddf905ad5f2f545987ce61270aa0a57705f1cfbe592ffbc649afb5c1137e8f69552388750e198
|
7
|
+
data.tar.gz: c40c023541aaf714d0bfaa0fbaa4034edcb81d2d8b627a05f4e696d9eaa165f5f1aff7596497c9b4ccb9bf19773b65bb1aa79c7ba19765f3c993eab9e260e864
|
data/lib/config/pagy.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Pagy initializer file (5.6.
|
3
|
+
# Pagy initializer file (5.6.6)
|
4
4
|
# Customize only what you really need and notice that the core 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
|
|
data/lib/javascripts/pagy.js
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
// This code should be OK also with very old browsers
|
4
4
|
|
5
|
-
// Container
|
5
|
+
// Container for the whole pagy stuff
|
6
6
|
function Pagy(){}
|
7
7
|
|
8
|
-
Pagy.version = '5.6.
|
8
|
+
Pagy.version = '5.6.6'
|
9
9
|
|
10
10
|
// Used by the waitForMe function
|
11
11
|
Pagy.delay = 100
|
@@ -54,6 +54,7 @@ Pagy.nav =
|
|
54
54
|
else if (typeof(item) === 'string') { html += fill(tags.active, item, label) }
|
55
55
|
}
|
56
56
|
html += tags.after
|
57
|
+
this.innerHTML = ''
|
57
58
|
this.insertAdjacentHTML('afterbegin', html)
|
58
59
|
lastWidth = width
|
59
60
|
}
|
data/lib/pagy/backend.rb
CHANGED
data/lib/pagy/calendar/day.rb
CHANGED
@@ -13,8 +13,8 @@ class Pagy
|
|
13
13
|
def setup_unit_vars
|
14
14
|
super
|
15
15
|
@months = self.class::MONTHS # number of months in the unit
|
16
|
-
@initial =
|
17
|
-
@final = add_months_to(
|
16
|
+
@initial = unit_starting_time_for(@starting)
|
17
|
+
@final = add_months_to(unit_starting_time_for(@ending), @months)
|
18
18
|
@pages = @last = (months_in(@final) - months_in(@initial)) / @months
|
19
19
|
@from = starting_time_for(@page)
|
20
20
|
@to = add_months_to(@from, @months)
|
@@ -22,16 +22,18 @@ class Pagy
|
|
22
22
|
|
23
23
|
# Starting time for the page
|
24
24
|
def starting_time_for(page)
|
25
|
-
add_months_to(@initial,
|
26
|
-
end
|
27
|
-
|
28
|
-
# Starting month of the unit including the passed month
|
29
|
-
def starting_month_including(month)
|
30
|
-
(@months * ((month - 1) / @months)) + 1 # remove 1 month for 0-11 calculations and add it back for 1-12 conversion
|
25
|
+
add_months_to(@initial, offset_units_for(page) * @months)
|
31
26
|
end
|
32
27
|
|
33
28
|
private
|
34
29
|
|
30
|
+
# Unit starting time for time
|
31
|
+
def unit_starting_time_for(time)
|
32
|
+
# remove 1 month for 0-11 calculations and add it back for 1-12 conversion
|
33
|
+
starting_month = (@months * ((time.month - 1) / @months)) + 1
|
34
|
+
new_time(time.year, starting_month)
|
35
|
+
end
|
36
|
+
|
35
37
|
# Number of months in time
|
36
38
|
def months_in(time)
|
37
39
|
(time.year * 12) + time.month
|
data/lib/pagy/calendar/week.rb
CHANGED
@@ -15,8 +15,8 @@ class Pagy # :nodoc:
|
|
15
15
|
def setup_unit_vars
|
16
16
|
setup_vars(offset: 0)
|
17
17
|
super
|
18
|
-
@initial =
|
19
|
-
@final =
|
18
|
+
@initial = unit_starting_time_for(@starting)
|
19
|
+
@final = unit_starting_time_for(@ending) + WEEK
|
20
20
|
@pages = @last = (@final - @initial).to_i / WEEK
|
21
21
|
@from = starting_time_for(@page)
|
22
22
|
@to = @from + WEEK
|
@@ -24,15 +24,15 @@ class Pagy # :nodoc:
|
|
24
24
|
|
25
25
|
# Starting time for the page
|
26
26
|
def starting_time_for(page)
|
27
|
-
@initial + (
|
27
|
+
@initial + (offset_units_for(page) * WEEK)
|
28
28
|
end
|
29
29
|
|
30
30
|
private
|
31
31
|
|
32
|
-
#
|
33
|
-
def
|
34
|
-
|
35
|
-
new_time(
|
32
|
+
# Unit starting time for time
|
33
|
+
def unit_starting_time_for(time)
|
34
|
+
starting_time = time - (((time.wday - @offset) * DAY) % WEEK)
|
35
|
+
new_time(starting_time.year, starting_time.month, starting_time.day)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
data/lib/pagy/calendar/year.rb
CHANGED
data/lib/pagy/calendar.rb
CHANGED
@@ -39,14 +39,9 @@ class Pagy # :nodoc:
|
|
39
39
|
localize(starting_time_for(page.to_i), opts) # page could be a string
|
40
40
|
end
|
41
41
|
|
42
|
-
# Period of the active page (used for nested units)
|
43
|
-
def active_period
|
44
|
-
[[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
|
45
|
-
end
|
46
|
-
|
47
42
|
protected
|
48
43
|
|
49
|
-
# Base class method for the setup of the unit variables
|
44
|
+
# Base class method for the setup of the unit variables (subclasses must implement it and call super)
|
50
45
|
def setup_unit_vars
|
51
46
|
raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
|
52
47
|
raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
|
@@ -63,8 +58,9 @@ class Pagy # :nodoc:
|
|
63
58
|
time.strftime(opts[:format])
|
64
59
|
end
|
65
60
|
|
66
|
-
#
|
67
|
-
|
61
|
+
# Number of units to offset from the @initial time, in order to get the ordered starting time for the page.
|
62
|
+
# Used in starting_time_for(page) with a logic equivalent to: @initial + (offset_units_for(page) * unit_time_length)
|
63
|
+
def offset_units_for(page)
|
68
64
|
@order == :asc ? page - 1 : @pages - page
|
69
65
|
end
|
70
66
|
|
@@ -73,6 +69,11 @@ class Pagy # :nodoc:
|
|
73
69
|
Time.new(year, month, day, 0, 0, 0, @utc_offset)
|
74
70
|
end
|
75
71
|
|
72
|
+
# Period of the active page (used internally for nested units)
|
73
|
+
def active_period
|
74
|
+
[[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
|
75
|
+
end
|
76
|
+
|
76
77
|
class << self
|
77
78
|
# Create a subclass instance by unit name (internal use)
|
78
79
|
def create(unit, vars)
|
@@ -84,6 +85,6 @@ class Pagy # :nodoc:
|
|
84
85
|
end
|
85
86
|
end
|
86
87
|
end
|
87
|
-
|
88
|
+
# Require the subclass files in UNITS (no custom unit at this point yet)
|
88
89
|
Calendar::UNITS.each { |unit| require "pagy/calendar/#{unit}" }
|
89
90
|
end
|
data/lib/pagy/extras/arel.rb
CHANGED
@@ -6,11 +6,13 @@ class Pagy # :nodoc:
|
|
6
6
|
module ArelExtra
|
7
7
|
private
|
8
8
|
|
9
|
+
# Return Pagy object and paginated collection/results
|
9
10
|
def pagy_arel(collection, vars = {})
|
10
11
|
pagy = Pagy.new(pagy_arel_get_vars(collection, vars))
|
11
12
|
[pagy, pagy_get_items(collection, pagy)]
|
12
13
|
end
|
13
14
|
|
15
|
+
# Sub-method called only by #pagy_arel: here for easy customization of variables by overriding
|
14
16
|
def pagy_arel_get_vars(collection, vars)
|
15
17
|
pagy_set_items_from_params(vars) if defined?(ItemsExtra)
|
16
18
|
vars[:count] ||= pagy_arel_count(collection)
|
@@ -18,6 +20,7 @@ class Pagy # :nodoc:
|
|
18
20
|
vars
|
19
21
|
end
|
20
22
|
|
23
|
+
# Count using Arel when grouping
|
21
24
|
def pagy_arel_count(collection)
|
22
25
|
if collection.group_values.empty?
|
23
26
|
# COUNT(*)
|
data/lib/pagy/extras/array.rb
CHANGED
data/lib/pagy/extras/calendar.rb
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
require 'pagy/calendar'
|
5
5
|
|
6
6
|
class Pagy # :nodoc:
|
7
|
-
#
|
7
|
+
# Add pagination filtering by calendar unit (:year, :quarter, :month, :week, :day) to the regular pagination
|
8
8
|
module CalendarExtra
|
9
9
|
# Additions for the Backend module
|
10
10
|
module Backend
|
@@ -12,12 +12,7 @@ class Pagy # :nodoc:
|
|
12
12
|
|
13
13
|
private
|
14
14
|
|
15
|
-
# Take a collection and a conf Hash with keys in
|
16
|
-
# The calendar is active by default, but it can be explicitly inactivated with `active: false`
|
17
|
-
# Return a hash with 3 items:
|
18
|
-
# 0. Array of pagy calendar unit objects
|
19
|
-
# 1. Pagy object
|
20
|
-
# 2. Array of results
|
15
|
+
# Take a collection and a conf Hash with keys in CONF_KEYS and return an array with 3 items: [calendar, pagy, results]
|
21
16
|
def pagy_calendar(collection, conf)
|
22
17
|
unless conf.is_a?(Hash) && (conf.keys - CONF_KEYS).empty? && conf.all? { |k, v| v.is_a?(Hash) || k == :active }
|
23
18
|
raise ArgumentError, "keys must be in #{CONF_KEYS.inspect} and object values must be Hashes; got #{conf.inspect}"
|
@@ -25,50 +20,45 @@ class Pagy # :nodoc:
|
|
25
20
|
|
26
21
|
conf[:pagy] = {} unless conf[:pagy] # use default Pagy object when omitted
|
27
22
|
calendar, collection = pagy_setup_calendar(collection, conf) unless conf.key?(:active) && !conf[:active]
|
28
|
-
pagy,
|
29
|
-
[calendar, pagy,
|
23
|
+
pagy, results = send(conf[:pagy][:backend] || :pagy, collection, conf[:pagy]) # use backend: :pagy when omitted
|
24
|
+
[calendar, pagy, results]
|
30
25
|
end
|
31
26
|
|
32
27
|
# Setup and return the calendar objects and the filtered collection
|
33
28
|
def pagy_setup_calendar(collection, conf)
|
34
|
-
units
|
29
|
+
units = Calendar::UNITS & conf.keys # get the units in time length desc order
|
30
|
+
raise ArgumentError, 'no calendar unit found in pagy_calendar configuration' if units.empty?
|
31
|
+
|
35
32
|
page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
|
36
33
|
units.each do |unit| # set all the :page_param vars for later deletion
|
37
|
-
unit_page_param
|
34
|
+
unit_page_param = :"#{unit}_#{page_param}"
|
38
35
|
conf[unit][:page_param] = unit_page_param
|
39
36
|
conf[unit][:page] = params[unit_page_param]
|
40
37
|
end
|
41
38
|
calendar = {}
|
42
|
-
|
39
|
+
last_obj = nil
|
43
40
|
units.each_with_index do |unit, index|
|
44
41
|
params_to_delete = units[(index + 1), units.size].map { |sub| conf[sub][:page_param] } + [page_param]
|
45
42
|
conf[unit][:params] = lambda do |params| # delete page_param from the sub-units
|
46
|
-
params_to_delete.each { |p| params.delete(p.to_s) } # Hash#except missing from 2.5 baseline
|
43
|
+
params_to_delete.each { |p| params.delete(p.to_s) } # Hash#except missing from ruby 2.5 baseline
|
47
44
|
params
|
48
45
|
end
|
49
|
-
conf[unit][:period] =
|
50
|
-
calendar[unit] = Calendar.send(:create, unit, conf[unit])
|
51
|
-
period = calendar[unit].active_period # set the period for the next unit
|
46
|
+
conf[unit][:period] = last_obj&.send(:active_period) || pagy_calendar_period(collection)
|
47
|
+
calendar[unit] = last_obj = Calendar.send(:create, unit, conf[unit])
|
52
48
|
end
|
53
|
-
[calendar, pagy_calendar_filter(collection,
|
49
|
+
[calendar, pagy_calendar_filter(collection, last_obj.from, last_obj.to)]
|
54
50
|
end
|
55
51
|
|
56
|
-
# This method must be implemented by the application
|
57
|
-
# It must return the the starting and ending local Time objects defining the calendar :period
|
52
|
+
# This method must be implemented by the application
|
58
53
|
def pagy_calendar_period(*)
|
59
|
-
|
60
|
-
|
61
|
-
'the starting and ending local Time objects array defining the calendar :period'
|
54
|
+
raise NoMethodError, 'the pagy_calendar_period method must be implemented by the application ' \
|
55
|
+
'(see https://ddnexus.github.io/pagy/extras/calendar#pagy_calendar_periodcollection)'
|
62
56
|
end
|
63
57
|
|
64
|
-
# This method must be implemented by the application
|
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}
|
58
|
+
# This method must be implemented by the application
|
67
59
|
def pagy_calendar_filter(*)
|
68
|
-
|
69
|
-
|
70
|
-
'collection filtered by a logic equivalent to '\
|
71
|
-
'{storage_time >= from && storage_time < to}'
|
60
|
+
raise NoMethodError, 'the pagy_calendar_filter method must be implemented by the application ' \
|
61
|
+
'(see https://ddnexus.github.io/pagy/extras/calendar#pagy_calendar_filtercollection-from-to)'
|
72
62
|
end
|
73
63
|
end
|
74
64
|
end
|
@@ -18,7 +18,7 @@ class Pagy # :nodoc:
|
|
18
18
|
# "350" => [1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
|
19
19
|
# "550" => [1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] }
|
20
20
|
# Notice: if :steps is false it will use the single {0 => @vars[:size]} size
|
21
|
-
def sequels(steps: @vars[:steps] || { 0 => @vars[:size] })
|
21
|
+
def sequels(steps: @vars[:steps] || { 0 => @vars[:size] }, **_)
|
22
22
|
raise VariableError.new(self, :steps, 'to define the 0 width', steps) unless steps.key?(0)
|
23
23
|
|
24
24
|
{}.tap do |sequels|
|
data/lib/pagy/extras/gearbox.rb
CHANGED
@@ -28,11 +28,11 @@ class Pagy # :nodoc:
|
|
28
28
|
@pages = @last = (if @count > (sum = gearbox_items.sum)
|
29
29
|
[((@count - sum).to_f / gearbox_items.last).ceil, 1].max + gearbox_items.count
|
30
30
|
else
|
31
|
-
pages
|
32
|
-
|
33
|
-
while
|
34
|
-
pages
|
35
|
-
|
31
|
+
pages = 0
|
32
|
+
remainder = @count
|
33
|
+
while remainder.positive?
|
34
|
+
pages += 1
|
35
|
+
remainder -= gearbox_items[pages - 1]
|
36
36
|
end
|
37
37
|
[pages, 1].max
|
38
38
|
end)
|
data/lib/pagy/extras/i18n.rb
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
class Pagy # :nodoc:
|
5
5
|
# Use ::I18n gem
|
6
6
|
module I18nExtra
|
7
|
-
# Frontend overriding
|
7
|
+
# Frontend overriding for translation
|
8
8
|
module Frontend
|
9
9
|
def pagy_t(key, **opts)
|
10
10
|
::I18n.t(key, **opts)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
# Calendar overriding
|
14
|
+
# Calendar overriding for localization (see also the block in the calendar section of the config/pagy.rb initializer)
|
15
15
|
module Calendar
|
16
16
|
def localize(time, opts)
|
17
17
|
::I18n.l(time, **opts)
|
data/lib/pagy/frontend.rb
CHANGED
@@ -77,7 +77,7 @@ class Pagy
|
|
77
77
|
# Similar to I18n.t: just ~18x faster using ~10x less memory
|
78
78
|
# (@pagy_locale explicitly initialized in order to avoid warning)
|
79
79
|
def pagy_t(key, opts = {})
|
80
|
-
Pagy::I18n.
|
80
|
+
Pagy::I18n.translate(@pagy_locale ||= nil, key, opts)
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
data/lib/pagy/i18n.rb
CHANGED
@@ -153,11 +153,12 @@ class Pagy
|
|
153
153
|
end
|
154
154
|
|
155
155
|
# Translate and pluralize the key with the locale DATA
|
156
|
-
def
|
156
|
+
def translate(locale, key, opts = {})
|
157
157
|
data, pluralize = DATA[locale]
|
158
158
|
translation = data[key] || (opts[:count] && data[key += ".#{pluralize.call(opts[:count])}"]) \
|
159
159
|
or return %([translation missing: "#{key}"])
|
160
160
|
translation.gsub(/%{[^}]+?}/) { |match| opts[:"#{match[2..-2]}"] || match }
|
161
161
|
end
|
162
|
+
alias t translate
|
162
163
|
end
|
163
164
|
end
|
data/lib/pagy.rb
CHANGED
@@ -5,7 +5,7 @@ require 'pathname'
|
|
5
5
|
|
6
6
|
# Core class
|
7
7
|
class Pagy
|
8
|
-
VERSION = '5.6.
|
8
|
+
VERSION = '5.6.6'
|
9
9
|
|
10
10
|
# Root pathname to get the path of Pagy files like templates or dictionaries
|
11
11
|
def self.root
|
@@ -44,7 +44,7 @@ class Pagy
|
|
44
44
|
end
|
45
45
|
|
46
46
|
# Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
|
47
|
-
def series(size: @vars[:size])
|
47
|
+
def series(size: @vars[:size], **_)
|
48
48
|
return [] if size.empty?
|
49
49
|
raise VariableError.new(self, :size, 'to contain 4 items >= 0', size) \
|
50
50
|
unless size.is_a?(Array) && size.size == 4 && size.all? { |num| !num.negative? rescue false } # rubocop:disable Style/RescueModifier
|
@@ -59,11 +59,11 @@ class Pagy
|
|
59
59
|
series = []
|
60
60
|
start = 1
|
61
61
|
if (left_gap_end - left_gap_start).positive?
|
62
|
-
series.push(*start
|
62
|
+
series.push(*start...left_gap_start, :gap)
|
63
63
|
start = left_gap_end + 1
|
64
64
|
end
|
65
65
|
if (right_gap_end - right_gap_start).positive?
|
66
|
-
series.push(*start
|
66
|
+
series.push(*start...right_gap_start, :gap)
|
67
67
|
start = right_gap_end + 1
|
68
68
|
end
|
69
69
|
series.push(*start..@last)
|
@@ -76,6 +76,7 @@ class Pagy
|
|
76
76
|
page.to_s
|
77
77
|
end
|
78
78
|
|
79
|
+
# Allow the customization of the output (overridden by the calendar extra)
|
79
80
|
def label
|
80
81
|
@page.to_s
|
81
82
|
end
|
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.6.
|
4
|
+
version: 5.6.6
|
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
|
+
date: 2021-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Agnostic pagination in plain ruby. It does it all. Better.
|
14
14
|
email:
|
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
126
|
- !ruby/object:Gem::Version
|
127
127
|
version: '0'
|
128
128
|
requirements: []
|
129
|
-
rubygems_version: 3.2.
|
129
|
+
rubygems_version: 3.2.32
|
130
130
|
signing_key:
|
131
131
|
specification_version: 4
|
132
132
|
summary: The kick-ass pagination ruby gem
|