next_page 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -1
- data/lib/next_page/pagination_attributes.rb +14 -3
- data/lib/next_page/paginator.rb +8 -5
- data/lib/next_page/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc0a222313043518cba95ab5cba8b04ba260a1668d570cae3f8e0450458d8773
|
4
|
+
data.tar.gz: 1e865c0c7de475e1edb754957db1ab6d4f72e863dc9baa995c90c27c8006ca99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4111d99ac3cca85231988f6dd665c9091416b3a9ad7b29dea0108dc8cd4617cdb38c0f3068d64586741365d463b1c3ea390f28b58199b1ea20fbc7098cf5192d
|
7
|
+
data.tar.gz: 80c2bcbdaa84c8ef48a1f680df6a56fa1607d097b01546c9f553983fd5c4fa171a031abcafc122591576b7b0c2304df65acab3e89cd92ad8e5faabc6d7fe0862
|
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/next_page.svg)](https://badge.fury.io/rb/next_page)
|
2
|
+
[![RuboCop](https://github.com/RockSolt/next_page/workflows/RuboCop/badge.svg)](https://github.com/RockSolt/next_page/actions?query=workflow%3ARuboCop)
|
3
|
+
|
1
4
|
# NextPage
|
2
5
|
Basic pagination for Rails controllers.
|
3
6
|
|
@@ -72,12 +75,18 @@ paginate_with default_limit: 12, instance_variable_name: 'data'
|
|
72
75
|
```
|
73
76
|
|
74
77
|
### Link Helpers
|
75
|
-
This gem does not do any rendering. It does provide helper methods for generating links. The resource will include the following additional methods:
|
78
|
+
This gem does not do any rendering. It does provide helper methods for generating links. The resource will include the following additional methods (when the request header Accept is `'application/vnd.api+json'`):
|
76
79
|
- current_page
|
77
80
|
- next_page
|
78
81
|
- total_pages
|
79
82
|
- per_page
|
80
83
|
|
84
|
+
#### Count Query
|
85
|
+
In some cases (such as grouping), calling count on the query does not provide an accurate representation. If that is the case, then there are two ways to override the default behavior:
|
86
|
+
- provide a count_query that can resolve the attributes
|
87
|
+
- specify the following attributes manually: current_page, total_count, and per_page
|
88
|
+
|
89
|
+
|
81
90
|
## Installation
|
82
91
|
Add this line to your application's Gemfile:
|
83
92
|
|
@@ -5,9 +5,15 @@ module NextPage
|
|
5
5
|
#
|
6
6
|
# Module PaginationAttributes adds in methods required for pagination links: current_page, next_page, and total_pages.
|
7
7
|
# It reads the offset and limit on the query to determine the values.
|
8
|
+
#
|
9
|
+
# In some cases the query will not support count. In that case, there are two ways to override the default behavior:
|
10
|
+
# - provide a count_query that can resolve the attributes
|
11
|
+
# - specify the following attributes manually: current_page, total_count, and per_page
|
8
12
|
module PaginationAttributes
|
13
|
+
attr_writer :count_query, :current_page, :total_count, :per_page
|
14
|
+
|
9
15
|
def current_page
|
10
|
-
@current_page ||= offset_value + 1
|
16
|
+
@current_page ||= count_query.offset_value + 1
|
11
17
|
end
|
12
18
|
|
13
19
|
def next_page
|
@@ -15,7 +21,7 @@ module NextPage
|
|
15
21
|
end
|
16
22
|
|
17
23
|
def total_count
|
18
|
-
@total_count ||= unscope(:limit).unscope(:offset).count
|
24
|
+
@total_count ||= count_query.unscope(:limit).unscope(:offset).count
|
19
25
|
end
|
20
26
|
|
21
27
|
def total_pages
|
@@ -23,7 +29,12 @@ module NextPage
|
|
23
29
|
end
|
24
30
|
|
25
31
|
def per_page
|
26
|
-
@per_page ||= limit_value
|
32
|
+
@per_page ||= count_query.limit_value
|
33
|
+
end
|
34
|
+
|
35
|
+
# checks first to see if an override query has been provided, then fails back to self
|
36
|
+
def count_query
|
37
|
+
@count_query || self
|
27
38
|
end
|
28
39
|
end
|
29
40
|
end
|
data/lib/next_page/paginator.rb
CHANGED
@@ -37,13 +37,10 @@ module NextPage
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def paginate_resource(data, params)
|
40
|
-
data
|
40
|
+
assign_pagination_attributes(data, params)
|
41
41
|
|
42
42
|
data = sorter.sort(data, params.fetch(:sort, default_sort))
|
43
|
-
|
44
|
-
limit = page_size(params[:page])
|
45
|
-
offset = page_number(params[:page]) - 1
|
46
|
-
data.limit(limit).offset(offset * limit)
|
43
|
+
data.limit(data.per_page).offset((data.current_page - 1) * data.per_page)
|
47
44
|
end
|
48
45
|
|
49
46
|
private
|
@@ -62,6 +59,12 @@ module NextPage
|
|
62
59
|
@default_sort ||= "-#{@model_class.primary_key}"
|
63
60
|
end
|
64
61
|
|
62
|
+
def assign_pagination_attributes(data, params)
|
63
|
+
data.extend(NextPage::PaginationAttributes)
|
64
|
+
data.per_page = page_size(params[:page])
|
65
|
+
data.current_page = page_number(params[:page])
|
66
|
+
end
|
67
|
+
|
65
68
|
def page_size(page)
|
66
69
|
if page.present? && page[:size].present?
|
67
70
|
page[:size]&.to_i
|
data/lib/next_page/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: next_page
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Todd Kummer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -106,14 +106,14 @@ dependencies:
|
|
106
106
|
requirements:
|
107
107
|
- - "~>"
|
108
108
|
- !ruby/object:Gem::Version
|
109
|
-
version:
|
109
|
+
version: 0.82.0
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
112
|
version_requirements: !ruby/object:Gem::Requirement
|
113
113
|
requirements:
|
114
114
|
- - "~>"
|
115
115
|
- !ruby/object:Gem::Version
|
116
|
-
version:
|
116
|
+
version: 0.82.0
|
117
117
|
- !ruby/object:Gem::Dependency
|
118
118
|
name: simplecov
|
119
119
|
requirement: !ruby/object:Gem::Requirement
|