jbuilder_pagination 0.0.1 → 1.0.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/.rspec +0 -1
- data/README.md +31 -10
- data/lib/jbuilder/pagination/pages.rb +23 -11
- data/lib/jbuilder/pagination/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d93468a455509e826177eaea7b177366e48fe053
|
4
|
+
data.tar.gz: c578b2e04c6de4ebe37d03a5f0d6bce262d8e305
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2739aa4ae51328eb4c552c51681601c1535a99aee191d748753ebeb303910182f0516d03735a1135d282a1ded9cd8029e806ca6e26d528b24bad91a143c109f2
|
7
|
+
data.tar.gz: 1a7073af47cfd214cab2511d6fd375bc65b9015865e262dfe57b2cdac7cfe779bab5bf983e453b841533daad066658f1baf13706910ea1f813bb89cfcb564510
|
data/.rspec
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
## Requirement
|
6
6
|
|
7
|
-
|
7
|
+
`JbuilderPagination` relies on a paginated collection with the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate).
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -24,21 +24,42 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
+
###### Kaminari examples
|
28
|
+
```ruby
|
29
|
+
#array
|
30
|
+
@posts = Kaminari.paginate_array([1, 2, 3]).page(3).per(1)
|
31
|
+
|
32
|
+
#active_record
|
33
|
+
@posts = Post.page(3).per(1)
|
34
|
+
```
|
35
|
+
|
36
|
+
###### WillPaginate examples
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
#array
|
40
|
+
@posts = [1,2,3].paginate(page: 3, per_page: 1)
|
41
|
+
|
42
|
+
#active_record
|
43
|
+
@posts = Post.page(3).per_page(1)
|
44
|
+
```
|
45
|
+
|
46
|
+
And then in your `*.json.jbuilder`
|
47
|
+
|
27
48
|
```ruby
|
28
49
|
json.links do
|
29
|
-
json.pages! @
|
50
|
+
json.pages! @posts, url: "http://example.com/posts", query_parameters: { additional: 'parameters' }
|
30
51
|
end
|
31
52
|
|
32
53
|
# =>
|
33
|
-
#
|
34
|
-
# "
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
# },
|
54
|
+
# "links": {
|
55
|
+
# "self": "http://example.com/posts?page[number]=3&page[size]=1&additional=parameters",
|
56
|
+
# "first": "http://example.com/posts?page[number]=1&page[size]=1&additional=parameters",
|
57
|
+
# "prev": "http://example.com/posts?page[number]=2&page[size]=1&additional=parameters",
|
58
|
+
# "next": "http://example.com/posts?page[number]=4&page[size]=1&additional=parameters",
|
59
|
+
# "last": "http://example.com/posts?page[number]=13&page[size]=1&additional=parameters"
|
60
|
+
# }
|
41
61
|
```
|
62
|
+
The options `url` and `query_parameters` are opcionals.
|
42
63
|
|
43
64
|
In case there is no pagination at all, `links` will be omitted.
|
44
65
|
|
@@ -1,29 +1,41 @@
|
|
1
1
|
class Jbuilder
|
2
|
+
ONE_PAGE = 1
|
3
|
+
|
2
4
|
def pages!(collection, options={})
|
3
|
-
return unless collection
|
5
|
+
return unless collection && is_paginated?(collection)
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
7
|
+
pages_from(collection).map do |key, value|
|
8
|
+
params = query_parameters(options).merge(page: { number: value, size: collection.size }).to_query
|
9
|
+
_set_value key, "#{options.fetch(:url, nil)}?#{params}"
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
12
13
|
private
|
13
14
|
|
14
15
|
def pages_from(collection)
|
15
|
-
return {} if collection.total_pages == 1
|
16
|
-
|
17
16
|
{}.tap do |pages|
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
pages[:self] = collection.current_page
|
18
|
+
return pages if collection.total_pages == ONE_PAGE
|
19
|
+
|
20
|
+
unless collection.current_page == ONE_PAGE
|
21
|
+
pages[:first] = ONE_PAGE
|
22
|
+
pages[:prev] = collection.current_page - ONE_PAGE
|
21
23
|
end
|
22
24
|
|
23
25
|
unless collection.current_page == collection.total_pages
|
26
|
+
pages[:next] = collection.current_page + ONE_PAGE
|
24
27
|
pages[:last] = collection.total_pages
|
25
|
-
pages[:next] = collection.current_page + 1
|
26
28
|
end
|
27
29
|
end
|
28
30
|
end
|
31
|
+
|
32
|
+
def query_parameters(options)
|
33
|
+
@query_parameters ||= options.fetch(:query_parameters, {})
|
34
|
+
end
|
35
|
+
|
36
|
+
def is_paginated?(collection)
|
37
|
+
collection.respond_to?(:current_page) &&
|
38
|
+
collection.respond_to?(:total_pages) &&
|
39
|
+
collection.respond_to?(:size)
|
40
|
+
end
|
29
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jbuilder_pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Bacarini
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jbuilder
|
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.4.
|
125
|
+
rubygems_version: 2.4.8
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: Jbuilder extension to allows pagination according to JSON API format
|