pagy 3.11.0 → 4.3.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 +10 -4
- data/lib/javascripts/pagy.js +1 -1
- data/lib/locales/bs.yml +24 -0
- data/lib/locales/hr.yml +24 -0
- data/lib/locales/sr.yml +23 -0
- data/lib/locales/utils/i18n.rb +3 -11
- data/lib/locales/utils/loader.rb +7 -10
- data/lib/locales/utils/p11n.rb +27 -20
- data/lib/pagy.rb +48 -26
- data/lib/pagy/backend.rb +4 -3
- data/lib/pagy/countless.rb +13 -10
- data/lib/pagy/exceptions.rb +8 -5
- data/lib/pagy/extras/arel.rb +3 -3
- data/lib/pagy/extras/array.rb +3 -3
- data/lib/pagy/extras/bootstrap.rb +54 -27
- data/lib/pagy/extras/bulma.rb +52 -34
- data/lib/pagy/extras/countless.rb +5 -7
- data/lib/pagy/extras/elasticsearch_rails.rb +27 -17
- data/lib/pagy/extras/foundation.rb +54 -29
- data/lib/pagy/extras/headers.rb +13 -9
- data/lib/pagy/extras/i18n.rb +8 -11
- data/lib/pagy/extras/items.rb +32 -29
- data/lib/pagy/extras/materialize.rb +50 -32
- data/lib/pagy/extras/metadata.rb +25 -20
- data/lib/pagy/extras/navs.rb +39 -18
- data/lib/pagy/extras/overflow.rb +50 -48
- data/lib/pagy/extras/searchkick.rb +26 -16
- data/lib/pagy/extras/semantic.rb +48 -27
- data/lib/pagy/extras/shared.rb +10 -6
- data/lib/pagy/extras/support.rb +10 -5
- data/lib/pagy/extras/trim.rb +11 -11
- data/lib/pagy/extras/uikit.rb +56 -31
- data/lib/pagy/frontend.rb +47 -29
- metadata +7 -7
- data/lib/locales/README.md +0 -35
- data/lib/pagy/extras/pagy_search.rb +0 -18
- data/pagy.gemspec +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66ae2dcfec4f4aefaa0a2531b5abe5e0d18b92a835edf6549251c0073ce2f38f
|
4
|
+
data.tar.gz: 295ca05e50dc171da7c184d7d3e8dd9e3b7eaed1cf7387959471a3f758f6d169
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d96430c17d8936a27942e3da7c0fbcff4d80372afd1f99b7a0c12b4ed5eb694aa1921404d31b77889078dc0fc59b4bbc5bfb0e4a6c418f3ae50837436bc17262
|
7
|
+
data.tar.gz: d7349c4e47d8b33cdadea01168d8a122ef36c6283164cf895dbcf197aca0f07c44437abce7e7d72b29d358f4d95b80586157d5bd5eeb4515e32b7001031329b8
|
data/lib/config/pagy.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
|
-
# Pagy initializer file (3.
|
3
|
+
# Pagy initializer file (4.3.0)
|
5
4
|
# Customize only what you really need and notice that Pagy works also without any of the following lines.
|
6
5
|
# Should you just cherry pick part of this file, please maintain the require-order of the extras
|
7
6
|
|
@@ -23,13 +22,18 @@
|
|
23
22
|
|
24
23
|
# Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects
|
25
24
|
# See https://ddnexus.github.io/pagy/extras/elasticsearch_rails
|
25
|
+
# default :pagy_search method: change only if you use
|
26
|
+
# also the searchkick extra that defines the same
|
27
|
+
# VARS[:elasticsearch_rails_search_method] = :pagy_search
|
26
28
|
# require 'pagy/extras/elasticsearch_rails'
|
27
29
|
|
28
30
|
# Searchkick extra: Paginate `Searchkick::Results` objects
|
29
31
|
# See https://ddnexus.github.io/pagy/extras/searchkick
|
32
|
+
# default :pagy_search method: change only if you use
|
33
|
+
# also the elasticsearch_rails extra that defines the same
|
34
|
+
# VARS[:searchkick_search_method] = :pagy_search
|
30
35
|
# require 'pagy/extras/searchkick'
|
31
36
|
|
32
|
-
|
33
37
|
# Frontend Extras
|
34
38
|
|
35
39
|
# Bootstrap extra: Add nav, nav_js and combo_nav_js helpers and templates for Bootstrap pagination
|
@@ -100,7 +104,9 @@
|
|
100
104
|
# Trim extra: Remove the page=1 param from links
|
101
105
|
# See https://ddnexus.github.io/pagy/extras/trim
|
102
106
|
# require 'pagy/extras/trim'
|
103
|
-
|
107
|
+
# after requiring it will trim by default
|
108
|
+
# set to false if you want to make :trim an opt-in variable
|
109
|
+
# Pagy::VARS[:trim] = true # default
|
104
110
|
|
105
111
|
|
106
112
|
# Pagy Variables
|
data/lib/javascripts/pagy.js
CHANGED
data/lib/locales/bs.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# :east_slavic pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
|
2
|
+
|
3
|
+
bs:
|
4
|
+
pagy:
|
5
|
+
|
6
|
+
item_name:
|
7
|
+
one: "stavka"
|
8
|
+
few: "stavke"
|
9
|
+
many: "stavki"
|
10
|
+
other: "stavki"
|
11
|
+
|
12
|
+
nav:
|
13
|
+
prev: "‹ Prethodna"
|
14
|
+
next: "Sljedeća ›"
|
15
|
+
gap: "…"
|
16
|
+
|
17
|
+
info:
|
18
|
+
no_items: "Nije pronađena %{item_name}"
|
19
|
+
single_page: "Prikazuje se <b>%{count}</b> %{item_name}"
|
20
|
+
multiple_pages: "Prikaz %{item_name} <b>%{from}-%{to}</b> od <b>%{count}</b> ukupno"
|
21
|
+
|
22
|
+
combo_nav_js: "Stranica %{page_input} od %{pages}"
|
23
|
+
|
24
|
+
items_selector_js: "Prikaži %{items_input} %{item_name} po stranici"
|
data/lib/locales/hr.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# :east_slavic pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
|
2
|
+
|
3
|
+
hr:
|
4
|
+
pagy:
|
5
|
+
|
6
|
+
item_name:
|
7
|
+
one: "stavka"
|
8
|
+
few: "stavke"
|
9
|
+
many: "stavki"
|
10
|
+
other: "stavki"
|
11
|
+
|
12
|
+
nav:
|
13
|
+
prev: "‹ Prethodna"
|
14
|
+
next: "Sljedeća ›"
|
15
|
+
gap: "…"
|
16
|
+
|
17
|
+
info:
|
18
|
+
no_items: "Nije pronađena %{item_name}"
|
19
|
+
single_page: "Prikazuje se <b>%{count}</b> %{item_name}"
|
20
|
+
multiple_pages: "Prikaz %{item_name} <b>%{from}-%{to}</b> od <b>%{count}</b> ukupno"
|
21
|
+
|
22
|
+
combo_nav_js: "Stranica %{page_input} od %{pages}"
|
23
|
+
|
24
|
+
items_selector_js: "Prikaži %{items_input} %{item_name} po stranici"
|
data/lib/locales/sr.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# :east_slavic pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
|
2
|
+
|
3
|
+
sr:
|
4
|
+
pagy:
|
5
|
+
item_name:
|
6
|
+
one: "ставка"
|
7
|
+
few: "ставкe"
|
8
|
+
many: "ставки"
|
9
|
+
other: "ставки"
|
10
|
+
|
11
|
+
nav:
|
12
|
+
prev: "‹ Претходна"
|
13
|
+
next: "Следећa ›"
|
14
|
+
gap: "…"
|
15
|
+
|
16
|
+
info:
|
17
|
+
no_items: "Нema пронађених %{item_name}"
|
18
|
+
single_page: "Приказује се <b>%{count}</b> %{item_name}"
|
19
|
+
multiple_pages: "Приказ %{item_name} <b>%{from}-%{to}</b> од <b>%{count}</b> укупно"
|
20
|
+
|
21
|
+
combo_nav_js: "Страниca %{page_input} од %{pages}"
|
22
|
+
|
23
|
+
items_selector_js: "Прикажи %{items_input} %{item_name} по страниcи"
|
data/lib/locales/utils/i18n.rb
CHANGED
@@ -1,25 +1,17 @@
|
|
1
1
|
# See https://ddnexus.github.io/pagy/api/frontend#i18n
|
2
|
-
# encoding: utf-8
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
4
|
# this file returns the I18n hash used as default alternative to the i18n gem
|
6
5
|
|
7
|
-
Pagy::DEPRECATED_LOCALES = {'pt-br' =>'pt-BR', 'se' => 'sv-SE'}
|
8
|
-
|
9
6
|
Hash.new{|h,_| h.first[1]}.tap do |i18n_hash| # first loaded locale used as default
|
10
7
|
i18n_hash.define_singleton_method(:load) do |*load_args|
|
11
8
|
# eval: we don't need to keep the loader proc in memory
|
12
9
|
eval(Pagy.root.join('locales', 'utils', 'loader.rb').read).call(i18n_hash, *load_args) #rubocop:disable Security/Eval
|
13
10
|
end
|
14
|
-
i18n_hash.define_singleton_method(:t) do |locale,
|
15
|
-
if Pagy::DEPRECATED_LOCALES.key?(locale)
|
16
|
-
new_locale = Pagy::DEPRECATED_LOCALES[locale]
|
17
|
-
$stderr.puts("WARNING: the Pagy locale '#{locale}' is deprecated; use '#{new_locale}' instead")
|
18
|
-
locale = new_locale
|
19
|
-
end
|
11
|
+
i18n_hash.define_singleton_method(:t) do |locale, key, **opts|
|
20
12
|
data, pluralize = self[locale]
|
21
|
-
translate = data[
|
22
|
-
translate.call(
|
13
|
+
translate = data[key] || opts[:count] && data[key+=".#{pluralize.call(opts[:count])}"] or return %([translation missing: "#{key}"])
|
14
|
+
translate.call(opts)
|
23
15
|
end
|
24
16
|
i18n_hash.load(locale: 'en')
|
25
17
|
end
|
data/lib/locales/utils/loader.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
3
|
# the whole file will be eval'ed/executed and gc-collected after returning/executing the loader proc
|
5
4
|
|
6
5
|
# eval: no need for the whole file in memory
|
7
|
-
plurals,
|
6
|
+
plurals, = eval Pagy.root.join('locales', 'utils', 'p11n.rb').read # rubocop:disable Security/Eval
|
8
7
|
|
9
8
|
# flatten the dictionary file nested keys
|
10
9
|
# convert each value to a simple ruby interpolation proc
|
11
10
|
flatten = lambda do |hash, key=''|
|
12
11
|
hash.each.reduce({}) do |h, (k, v)|
|
13
|
-
v.is_a?(Hash)
|
14
|
-
|
15
|
-
|
12
|
+
if v.is_a?(Hash)
|
13
|
+
h.merge! flatten.call(v, "#{key}#{k}.")
|
14
|
+
else
|
15
|
+
code = %({"#{key}#{k}" => lambda{|vars|"#{v.gsub(/%{[^}]+?}/){|m| "\#{vars[:#{m[2..-2]}]||'#{m}'}" }}"}})
|
16
|
+
h.merge! eval(code) # rubocop:disable Security/Eval
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
@@ -20,11 +22,6 @@ flatten = lambda do |hash, key=''|
|
|
20
22
|
lambda do |i18n, *args|
|
21
23
|
i18n.clear
|
22
24
|
args.each do |arg|
|
23
|
-
if Pagy::DEPRECATED_LOCALES.key?(arg[:locale])
|
24
|
-
new_locale = Pagy::DEPRECATED_LOCALES[arg[:locale]]
|
25
|
-
$stderr.puts("WARNING: the Pagy locale '#{arg[:locale]}' is deprecated; use '#{new_locale}' instead")
|
26
|
-
arg[:locale] = new_locale
|
27
|
-
end
|
28
25
|
arg[:filepath] ||= Pagy.root.join('locales', "#{arg[:locale]}.yml")
|
29
26
|
arg[:pluralize] ||= plurals[arg[:locale]]
|
30
27
|
hash = YAML.load(File.read(arg[:filepath], encoding: 'UTF-8')) #rubocop:disable Security/YAMLLoad
|
data/lib/locales/utils/p11n.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
# See https://ddnexus.github.io/pagy/api/frontend#i18n
|
2
|
-
# encoding: utf-8
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
4
|
# This file adds support for multiple built-in plualization types.
|
@@ -18,46 +17,51 @@ from12to14 = [12,13,14].freeze
|
|
18
17
|
# Each proc may apply to one or more locales below.
|
19
18
|
# Pluralization logic adapted from https://github.com/svenfuchs/rails-i18n
|
20
19
|
p11n = {
|
21
|
-
one_other:
|
20
|
+
one_other: -> (n){ n == 1 ? 'one' : 'other' }, # default
|
22
21
|
|
23
22
|
east_slavic: lambda do |n|
|
24
23
|
n ||= 0
|
25
24
|
mod10 = n % 10
|
26
25
|
mod100 = n % 100
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
case
|
28
|
+
when mod10 == 1 && mod100 != 11 then 'one'
|
29
|
+
when from2to4.include?(mod10) && !from12to14.include?(mod100) then 'few'
|
30
|
+
when mod10 == 0 || from5to9.include?(mod10) || from11to14.include?(mod100) then 'many' # rubocop:disable Style/NumericPredicate
|
31
|
+
else 'other'
|
32
32
|
end
|
33
33
|
end,
|
34
34
|
|
35
35
|
west_slavic: lambda do |n|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
case n
|
37
|
+
when 1 then 'one'
|
38
|
+
when 2, 3, 4 then 'few'
|
39
|
+
else 'other'
|
40
|
+
end
|
40
41
|
end,
|
41
42
|
|
42
43
|
one_two_other: lambda do |n|
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
case n
|
45
|
+
when 1 then 'one'
|
46
|
+
when 2 then 'two'
|
47
|
+
else 'other'
|
46
48
|
end
|
47
49
|
end,
|
48
50
|
|
49
|
-
one_upto_two_other:
|
51
|
+
one_upto_two_other: -> (n){ n && n >= 0 && n < 2 ? 'one' : 'other' },
|
50
52
|
|
51
|
-
other:
|
53
|
+
other: -> (*){ 'other' },
|
52
54
|
|
53
55
|
polish: lambda do |n|
|
54
56
|
n ||= 0
|
55
|
-
mod10
|
57
|
+
mod10 = n % 10
|
56
58
|
mod100 = n % 100
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
59
|
+
|
60
|
+
case
|
61
|
+
when n == 1 then 'one'
|
62
|
+
when from2to4.include?(mod10) && !from12to14.include?(mod100) then 'few'
|
63
|
+
when (from0to1 + from5to9).include?(mod10) || from12to14.include?(mod100) then 'many'
|
64
|
+
else 'other'
|
61
65
|
end
|
62
66
|
end
|
63
67
|
}
|
@@ -67,14 +71,17 @@ p11n = {
|
|
67
71
|
# The default pluralization for locales not explicitly listed here
|
68
72
|
# is the :one_other pluralization proc (used for English)
|
69
73
|
plurals = Hash.new(p11n[:one_other]).tap do |hash|
|
74
|
+
hash['bs'] = p11n[:east_slavic]
|
70
75
|
hash['cs'] = p11n[:west_slavic]
|
71
76
|
hash['id'] = p11n[:other]
|
72
77
|
hash['fr'] = p11n[:one_upto_two_other]
|
78
|
+
hash['hr'] = p11n[:east_slavic]
|
73
79
|
hash['ja'] = p11n[:other]
|
74
80
|
hash['km'] = p11n[:other]
|
75
81
|
hash['ko'] = p11n[:other]
|
76
82
|
hash['pl'] = p11n[:polish]
|
77
83
|
hash['ru'] = p11n[:east_slavic]
|
84
|
+
hash['sr'] = p11n[:east_slavic]
|
78
85
|
hash['sv'] = p11n[:one_two_other]
|
79
86
|
hash['sv-SE'] = p11n[:one_two_other]
|
80
87
|
hash['tr'] = p11n[:other]
|
data/lib/pagy.rb
CHANGED
@@ -1,48 +1,70 @@
|
|
1
1
|
# See Pagy API documentation: https://ddnexus.github.io/pagy/api/pagy
|
2
|
-
# encoding: utf-8
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
4
|
require 'pathname'
|
6
5
|
|
7
|
-
|
6
|
+
# main class
|
7
|
+
class Pagy
|
8
|
+
VERSION = '4.3.0'
|
8
9
|
|
9
10
|
# Root pathname to get the path of Pagy files like templates or dictionaries
|
10
|
-
def self.root
|
11
|
+
def self.root
|
12
|
+
@root ||= Pathname.new(__dir__).freeze
|
13
|
+
end
|
11
14
|
|
12
15
|
# default vars
|
13
|
-
VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page,
|
16
|
+
VARS = { page: 1, items: 20, outset: 0, size: [1, 4, 4, 1], page_param: :page, # rubocop:disable Style/MutableConstant
|
17
|
+
params: {}, anchor: '', link_extra: '', i18n_key: 'pagy.item_name', cycle: false }
|
14
18
|
|
15
19
|
attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next
|
16
20
|
|
21
|
+
INSTANCE_VARS_MIN = { count: 0, items: 1, page: 1, outset: 0 }.freeze
|
22
|
+
|
17
23
|
# Merge and validate the options, do some simple arithmetic and set the instance variables
|
18
24
|
def initialize(vars)
|
19
|
-
@vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' })
|
20
|
-
|
21
|
-
|
22
|
-
|
25
|
+
@vars = VARS.merge( vars.delete_if{|_,v| v.nil? || v == '' } )
|
26
|
+
|
27
|
+
INSTANCE_VARS_MIN.each do |name,min|
|
28
|
+
raise VariableError.new(self), "expected :#{name} >= #{min}; got #{@vars[name].inspect}" \
|
29
|
+
unless @vars[name] && instance_variable_set(:"@#{name}", @vars[name].to_i) >= min
|
23
30
|
end
|
24
|
-
@pages = @last = [(@count.to_f / @items).ceil, 1].max
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
@
|
29
|
-
@
|
30
|
-
@
|
31
|
-
@
|
31
|
+
@pages = @last = [(@count.to_f / @items).ceil, 1].max
|
32
|
+
raise OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}" \
|
33
|
+
if @page > @last
|
34
|
+
|
35
|
+
@offset = @items * (@page - 1) + @outset
|
36
|
+
@items = @count - ((@pages - 1) * @items) if @page == @last && @count.positive?
|
37
|
+
@from = @count.zero? ? 0 : @offset + 1 - @outset
|
38
|
+
@to = @count.zero? ? 0 : @offset + @items - @outset
|
39
|
+
@prev = (@page - 1 unless @page == 1)
|
40
|
+
@next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
|
32
41
|
end
|
33
42
|
|
34
43
|
# Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
|
35
44
|
def series(size=@vars[:size])
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
45
|
+
return [] if size.empty?
|
46
|
+
raise VariableError.new(self), "expected 4 items >= 0 in :size; got #{size.inspect}" \
|
47
|
+
unless size.size == 4 && size.all?{ |num| !num.negative? rescue false } # rubocop:disable Style/RescueModifier
|
48
|
+
# This algorithm is up to ~5x faster and ~2.3x lighter than the previous one (pagy < 4.3)
|
49
|
+
left_gap_start = 1 + size[0]
|
50
|
+
left_gap_end = @page - size[1] - 1
|
51
|
+
right_gap_start = @page + size[2] + 1
|
52
|
+
right_gap_end = @last - size[3]
|
53
|
+
left_gap_end = right_gap_end if left_gap_end > right_gap_end
|
54
|
+
right_gap_start = left_gap_start if left_gap_start > right_gap_start
|
55
|
+
series = []
|
56
|
+
start = 1
|
57
|
+
if (left_gap_end - left_gap_start).positive?
|
58
|
+
series.push(*start..(left_gap_start - 1), :gap)
|
59
|
+
start = left_gap_end + 1
|
60
|
+
end
|
61
|
+
if (right_gap_end - right_gap_start).positive?
|
62
|
+
series.push(*start..(right_gap_start - 1), :gap)
|
63
|
+
start = right_gap_end + 1
|
64
|
+
end
|
65
|
+
series.push(*start..@last)
|
66
|
+
series[series.index(@page)] = @page.to_s
|
67
|
+
series
|
46
68
|
end
|
47
69
|
|
48
70
|
end
|
data/lib/pagy/backend.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
# See Pagy::Backend API documentation: https://ddnexus.github.io/pagy/api/backend
|
2
|
-
# encoding: utf-8
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
4
|
class Pagy
|
@@ -8,12 +7,14 @@ class Pagy
|
|
8
7
|
|
9
8
|
# See also the extras if you need specialized methods to paginate Arrays or other collections
|
10
9
|
|
11
|
-
|
10
|
+
|
11
|
+
module Backend
|
12
|
+
private # the whole module is private so no problem with including it in a controller
|
12
13
|
|
13
14
|
# Return Pagy object and items
|
14
15
|
def pagy(collection, vars={})
|
15
16
|
pagy = Pagy.new(pagy_get_vars(collection, vars))
|
16
|
-
|
17
|
+
[ pagy, pagy_get_items(collection, pagy) ]
|
17
18
|
end
|
18
19
|
|
19
20
|
# Sub-method called only by #pagy: here for easy customization of variables by overriding
|
data/lib/pagy/countless.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
3
|
require 'pagy'
|
@@ -7,25 +6,29 @@ class Pagy
|
|
7
6
|
|
8
7
|
class Countless < Pagy
|
9
8
|
|
9
|
+
INSTANCE_VARS_MIN = { items: 1, page: 1, outset: 0 }.freeze
|
10
|
+
|
10
11
|
# Merge and validate the options, do some simple arithmetic and set a few instance variables
|
11
|
-
def initialize(vars={})
|
12
|
+
def initialize(vars={}) # rubocop:disable Lint/MissingSuper
|
12
13
|
@vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars (can be overridden)
|
13
|
-
|
14
|
-
(
|
15
|
-
|
14
|
+
INSTANCE_VARS_MIN.each do |k,min| # validate instance variables
|
15
|
+
raise VariableError.new(self), "expected :#{k} >= #{min}; got #{@vars[k].inspect}" \
|
16
|
+
unless @vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min
|
16
17
|
end
|
17
18
|
@offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
|
18
19
|
end
|
19
20
|
|
20
21
|
# Finalize the instance variables based on the fetched items
|
21
22
|
def finalize(fetched)
|
22
|
-
|
23
|
+
raise OverflowError.new(self), "page #{@page} got no items" \
|
24
|
+
if fetched.zero? && @page > 1
|
25
|
+
|
23
26
|
@pages = @last = (fetched > @items ? @page + 1 : @page) # set the @pages and @last
|
24
|
-
@items = fetched if fetched < @items && fetched
|
25
|
-
@from = fetched
|
26
|
-
@to = fetched
|
27
|
+
@items = fetched if fetched < @items && fetched.positive? # adjust items for last non-empty page
|
28
|
+
@from = fetched.zero? ? 0 : @offset + 1 - @outset # page begins from item
|
29
|
+
@to = fetched.zero? ? 0 : @offset + @items - @outset # page ends to item
|
27
30
|
@prev = (@page-1 unless @page == 1) # nil if no prev page
|
28
|
-
@next = @page == @last ? (1 if @vars[:cycle]) : @page+1
|
31
|
+
@next = @page == @last ? (1 if @vars[:cycle]) : @page + 1 # nil if no next page, 1 if :cycle
|
29
32
|
self
|
30
33
|
end
|
31
34
|
|