pagy 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/config/pagy.rb +4 -0
- data/lib/pagy.rb +3 -3
- data/lib/pagy/countless.rb +30 -0
- data/lib/pagy/extras/countless.rb +35 -0
- data/lib/pagy/extras/items.rb +16 -2
- data/lib/pagy/extras/overflow.rb +32 -11
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bc238e37f6ae65c1581b464c1ac3b2d3d96a1168aea4d05e6f4c093a6d2a574
|
4
|
+
data.tar.gz: a3cbe116d1279fce48a91ba0c78b1a59d22445fe37f17aff5660305c9b5dc0d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6c0979f616fd8cac7c20ef6f6242ddd8a1a1099fa6b2cf0b3678b180705bc2e74e8e25e29d761a0f497c99aa3120783661f73044dc35bea701ea97c637526c1
|
7
|
+
data.tar.gz: db8389ae56438687fb038915e4a7406d263797f05718fe22bad9b67ec3002f7befd7f21777a09d62023ed1fc1fa74ee50dc82619b685f79419fb9abcafd3e137
|
data/lib/config/pagy.rb
CHANGED
@@ -12,6 +12,10 @@
|
|
12
12
|
# See https://ddnexus.github.io/pagy/extras/array
|
13
13
|
# require 'pagy/extras/array'
|
14
14
|
|
15
|
+
# Countless: Paginate without any count, saving one query per rendering
|
16
|
+
# See https://ddnexus.github.io/pagy/extras/countless
|
17
|
+
# require 'pagy/extras/countless'
|
18
|
+
|
15
19
|
# Elasticsearch Rails: Paginate `ElasticsearchRails::Results` objects efficiently, avoiding expensive object-wrapping and without overriding.
|
16
20
|
# See https://ddnexus.github.io/pagy/extras/elasticsearch_rails
|
17
21
|
# require 'pagy/extras/elasticsearch_rails'
|
data/lib/pagy.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
require 'pathname'
|
5
5
|
|
6
|
-
class Pagy ; VERSION = '1.
|
6
|
+
class Pagy ; VERSION = '1.1.0'
|
7
7
|
|
8
8
|
class OverflowError < StandardError; attr_reader :pagy; def initialize(pagy) @pagy = pagy end; end
|
9
9
|
|
@@ -15,7 +15,7 @@ class Pagy ; VERSION = '1.0.0'
|
|
15
15
|
|
16
16
|
attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next
|
17
17
|
|
18
|
-
# Merge and validate the options, do some simple
|
18
|
+
# Merge and validate the options, do some simple arithmetic and set the instance variables
|
19
19
|
def initialize(vars)
|
20
20
|
@vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars
|
21
21
|
{ count:0, items:1, outset:0, page:1 }.each do |k,min| # validate instance variables
|
@@ -34,8 +34,8 @@ class Pagy ; VERSION = '1.0.0'
|
|
34
34
|
|
35
35
|
# Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
|
36
36
|
def series(size=@vars[:size])
|
37
|
+
(series = []) and size.empty? and return series
|
37
38
|
4.times{|i| (size[i]>=0 rescue nil) or raise(ArgumentError, "expected 4 items >= 0 in :size; got #{size.inspect}")}
|
38
|
-
series = []
|
39
39
|
[*0..size[0], *@page-size[1]..@page+size[2], *@last-size[3]+1..@last+1].sort!.each_cons(2) do |a, b|
|
40
40
|
if a<0 || a==b || a>@last # skip out of range and duplicates
|
41
41
|
elsif a+1 == b; series.push(a) # no gap -> no additions
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'pagy'
|
2
|
+
|
3
|
+
class Pagy
|
4
|
+
|
5
|
+
class Countless < Pagy
|
6
|
+
|
7
|
+
# Merge and validate the options, do some simple arithmetic and set a few instance variables
|
8
|
+
def initialize(vars={})
|
9
|
+
@vars ||= VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars (can be overridden)
|
10
|
+
{ items:1, outset:0, page:1 }.each do |k,min| # validate instance variables
|
11
|
+
(@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \
|
12
|
+
or raise(ArgumentError, "expected :#{k} >= #{min}; got #{@vars[k].inspect}")
|
13
|
+
end
|
14
|
+
@offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Finalize the instance variables based on the retrieved items
|
18
|
+
def finalize(items)
|
19
|
+
items == 0 && @page > 1 and raise(OverflowError.new(self), "page #{@page} got no items")
|
20
|
+
@pages = @last = (items > @items ? @page + 1 : @page) # set the @pages and @last
|
21
|
+
@items = items if items < @items && items > 0 # adjust items for last non-empty page
|
22
|
+
@from = items == 0 ? 0 : @offset+1 - @outset # page begins from item
|
23
|
+
@to = items == 0 ? 0 : @offset + @items - @outset # page ends to item
|
24
|
+
@prev = (@page-1 unless @page == 1) # nil if no prev page
|
25
|
+
@next = (@page+1 unless @page == @last) # nil if no next page
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/countless
|
2
|
+
|
3
|
+
require 'pagy/countless'
|
4
|
+
|
5
|
+
class Pagy
|
6
|
+
|
7
|
+
# used by the items extra
|
8
|
+
COUNTLESS = true
|
9
|
+
|
10
|
+
module Backend ; private # the whole module is private so no problem with including it in a controller
|
11
|
+
|
12
|
+
# Return Pagy object and items
|
13
|
+
def pagy_countless(collection, vars={})
|
14
|
+
pagy = Pagy::Countless.new(pagy_countless_get_vars(collection, vars))
|
15
|
+
return pagy, pagy_countless_get_items(collection, pagy)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Sub-method called only by #pagy_countless: here for easy customization of variables by overriding
|
19
|
+
def pagy_countless_get_vars(_collection, vars)
|
20
|
+
# Return the merged variables to initialize the Pagy object
|
21
|
+
{ page: params[vars[:page_param]||VARS[:page_param]] }.merge!(vars)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Sub-method called only by #pagy_countless: here for easy customization of record-extraction by overriding
|
25
|
+
def pagy_countless_get_items(collection, pagy)
|
26
|
+
# This should work with ActiveRecord, Sequel, Mongoid...
|
27
|
+
items = collection.offset(pagy.offset).limit(pagy.items + 1).to_a
|
28
|
+
items_size = items.size
|
29
|
+
items.pop if items_size == pagy.items + 1
|
30
|
+
pagy.finalize(items_size) # finalize may adjust pagy.items, so must be used after checking the size
|
31
|
+
items
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/lib/pagy/extras/items.rb
CHANGED
@@ -10,14 +10,28 @@ class Pagy
|
|
10
10
|
# Handle a custom number of :items from params
|
11
11
|
module Backend ; private
|
12
12
|
|
13
|
-
|
14
|
-
def pagy_get_vars_with_items(collection, vars)
|
13
|
+
def pagy_with_items(vars)
|
15
14
|
vars[:items] ||= (items = params[vars[:items_param] || VARS[:items_param]]) && # :items from :items_param
|
16
15
|
[items&.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
|
16
|
+
end
|
17
|
+
|
18
|
+
alias_method :pagy_get_vars_without_items, :pagy_get_vars
|
19
|
+
def pagy_get_vars_with_items(collection, vars)
|
20
|
+
pagy_with_items(vars)
|
17
21
|
pagy_get_vars_without_items(collection, vars)
|
18
22
|
end
|
19
23
|
alias_method :pagy_get_vars, :pagy_get_vars_with_items
|
20
24
|
|
25
|
+
# support for countless extra
|
26
|
+
if defined?(Pagy::COUNTLESS) # defined in the countless extra
|
27
|
+
alias_method :pagy_countless_get_vars_without_items, :pagy_countless_get_vars
|
28
|
+
def pagy_countless_get_vars_with_items(collection, vars)
|
29
|
+
pagy_with_items(vars)
|
30
|
+
pagy_countless_get_vars_without_items(collection, vars)
|
31
|
+
end
|
32
|
+
alias_method :pagy_countless_get_vars, :pagy_countless_get_vars_with_items
|
33
|
+
end
|
34
|
+
|
21
35
|
end
|
22
36
|
|
23
37
|
module Frontend
|
data/lib/pagy/extras/overflow.rb
CHANGED
@@ -3,9 +3,9 @@
|
|
3
3
|
|
4
4
|
class Pagy
|
5
5
|
|
6
|
-
VARS[:overflow] = :
|
6
|
+
VARS[:overflow] = :empty_page
|
7
7
|
|
8
|
-
def overflow?;
|
8
|
+
def overflow?; !!@overflow end
|
9
9
|
|
10
10
|
module Overflow
|
11
11
|
|
@@ -29,20 +29,41 @@ class Pagy
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
module Series
|
33
|
+
def series(size=@vars[:size])
|
34
|
+
@page = @last # series for last page
|
35
|
+
super(size).tap do |s| # call original series
|
36
|
+
s[s.index(@page.to_s)] = @page # string to integer (i.e. no current page)
|
37
|
+
@page = @vars[:page] # restore the actual page
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
32
42
|
end
|
43
|
+
prepend Overflow
|
33
44
|
|
34
|
-
|
45
|
+
# support for Pagy::Countless
|
46
|
+
if defined?(Pagy::Countless)
|
47
|
+
module CountlessOverflow
|
35
48
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
49
|
+
def finalize(items)
|
50
|
+
super
|
51
|
+
rescue OverflowError
|
52
|
+
@overflow = true # add the overflow flag
|
53
|
+
case @vars[:overflow]
|
54
|
+
when :exception
|
55
|
+
raise # same as without the extra
|
56
|
+
when :empty_page
|
57
|
+
@offset = @items = @from = @to = 0 # vars relative to the actual page
|
58
|
+
@vars[:size] = [] # no page in the series
|
59
|
+
self
|
60
|
+
else
|
61
|
+
raise ArgumentError, "expected :overflow variable in [:empty_page, :exception]; got #{@vars[:overflow].inspect}"
|
62
|
+
end
|
41
63
|
end
|
42
|
-
end
|
43
64
|
|
65
|
+
end
|
66
|
+
Countless.prepend CountlessOverflow
|
44
67
|
end
|
45
68
|
|
46
|
-
prepend Overflow
|
47
|
-
|
48
69
|
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: 1.
|
4
|
+
version: 1.1.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: 2018-11-
|
11
|
+
date: 2018-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: 'Agnostic pagination in plain ruby: it works with any framework, ORM
|
14
14
|
and DB type, with all kinds of collections, even pre-paginated, scopes, Arrays,
|
@@ -36,9 +36,11 @@ files:
|
|
36
36
|
- lib/locales/zh-cn.yml
|
37
37
|
- lib/pagy.rb
|
38
38
|
- lib/pagy/backend.rb
|
39
|
+
- lib/pagy/countless.rb
|
39
40
|
- lib/pagy/extras/array.rb
|
40
41
|
- lib/pagy/extras/bootstrap.rb
|
41
42
|
- lib/pagy/extras/bulma.rb
|
43
|
+
- lib/pagy/extras/countless.rb
|
42
44
|
- lib/pagy/extras/elasticsearch_rails.rb
|
43
45
|
- lib/pagy/extras/foundation.rb
|
44
46
|
- lib/pagy/extras/i18n.rb
|