jbuilder_pagination 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5e96a670102c2f927e59e21c26d90f9b4d2d070
4
- data.tar.gz: ee270b6e1ebcd7ac567aa3c5a43e4f18083b5893
3
+ metadata.gz: d93468a455509e826177eaea7b177366e48fe053
4
+ data.tar.gz: c578b2e04c6de4ebe37d03a5f0d6bce262d8e305
5
5
  SHA512:
6
- metadata.gz: 77db3f4d5c32df65b5c68ee9a387b3038c16571c8827d9ce8741517531d96b1302f127c391e24e452b0072660bbc39a41d800af60594e5e7ac707a4692ff48ab
7
- data.tar.gz: d4474e11826257048f4261475af2bd28a370012c162c130848f5fd43fba3bc032b83072b181f70791881ae66cb0ba61f1460b911f449e71299c1c962ce38d806
6
+ metadata.gz: 2739aa4ae51328eb4c552c51681601c1535a99aee191d748753ebeb303910182f0516d03735a1135d282a1ded9cd8029e806ca6e26d528b24bad91a143c109f2
7
+ data.tar.gz: 1a7073af47cfd214cab2511d6fd375bc65b9015865e262dfe57b2cdac7cfe779bab5bf983e453b841533daad066658f1baf13706910ea1f813bb89cfcb564510
data/.rspec CHANGED
@@ -1,2 +1 @@
1
- --format documentation
2
1
  --color
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ## Requirement
6
6
 
7
- It is based on [will_paginate](https://github.com/mislav/will_paginate).
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! @servers, url: "https://api.example.com/v1/servers"
50
+ json.pages! @posts, url: "http://example.com/posts", query_parameters: { additional: 'parameters' }
30
51
  end
31
52
 
32
53
  # =>
33
- # "links": {
34
- # "pages": {
35
- # "first": "https://api.example.com/v1/servers/v1/servers?page=1&per_page=1",
36
- # "prev": "https://api.example.com/v1/servers/v1/servers?page=1&per_page=1",
37
- # "last": "https://api.example.com/v1/servers/v1/servers?page=3&per_page=1",
38
- # "next": "https://api.example.com/v1/servers/v1/servers?page=3&per_page=1"
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
- pages do
6
- pages_from(collection).map do |k, v|
7
- _set_value k, "#{options[:url]}?page=#{v}&per_page=#{collection.per_page}"
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
- unless collection.current_page == 1
19
- pages[:first] = 1
20
- pages[:prev] = collection.current_page - 1
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
@@ -1,5 +1,5 @@
1
1
  module Jbuilder
2
2
  class Pagination
3
- VERSION = "0.0.1"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  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.1
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-04-23 00:00:00.000000000 Z
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.6
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