json_api_toolbox 0.7.2 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +20 -0
- data/json_api_toolbox.gemspec +3 -0
- data/lib/json_api_toolbox/paginable.rb +44 -0
- data/lib/json_api_toolbox/version.rb +1 -1
- data/lib/json_api_toolbox.rb +1 -0
- data/lib/renderizable.rb +1 -1
- metadata +45 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ff5b802b738effc049540070f3a5848a44fec21
|
4
|
+
data.tar.gz: 82b1ef0fd90a974257dcd53a07eacb61b1bd8314
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1a12a825d8952735be272a44f28decc8ea3a62e7ad69d927dc642e2093de1be621d5a86d576abcf537bea430c1049672b6e975bb6971bb3602ae4bfb8057f2e
|
7
|
+
data.tar.gz: 5baafad9b4756f550ee173ab4f8f30b5b74e5a674d01c6db17af39ca809ea7744672fa4e27f0a456a19f1d5785599786f21997ea4dc6de84cad6003b9c1d50a2
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [0.8.0] - 2017-12-15
|
10
|
+
### Added
|
11
|
+
- Added Module Paginable with pagination links and meta
|
12
|
+
|
9
13
|
## [0.7.0] - 2017-12-11
|
10
14
|
### Added
|
11
15
|
- Added Enum Serializable
|
data/README.md
CHANGED
@@ -51,6 +51,26 @@ new Hash, the key must be 'Hash' and the value must be your Serializable.
|
|
51
51
|
render_object(QuotaGap.all, class: { Hash: SerializableQuotaGap })
|
52
52
|
```
|
53
53
|
|
54
|
+
#Paginable
|
55
|
+
|
56
|
+
To render a collection with a json api pagination, you will need to send a Hash
|
57
|
+
that contains two keys called 'links' and 'meta', and its values must be Hashs.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class MyController
|
61
|
+
include JsonApiToolbox::Paginable
|
62
|
+
|
63
|
+
def index
|
64
|
+
collection = MyModel
|
65
|
+
.all
|
66
|
+
.paginate(page: params[:page],
|
67
|
+
per_page: params[:per_page])
|
68
|
+
render_object(collection, links: pagination(collection),
|
69
|
+
meta: pagination_meta(collection))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
54
74
|
##ShareExamplesForControllers
|
55
75
|
|
56
76
|
it_behaves_like 'a http method'
|
data/json_api_toolbox.gemspec
CHANGED
@@ -19,9 +19,12 @@ Gem::Specification.new do |spec|
|
|
19
19
|
end
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
+
spec.add_dependency 'actionpack', '>= 4.0'
|
23
|
+
spec.add_dependency 'activerecord', '>= 4.0'
|
22
24
|
spec.add_dependency 'activesupport', '>= 4.0'
|
23
25
|
spec.add_dependency 'jsonapi-serializable', '>= 0.3.0'
|
24
26
|
spec.add_dependency 'rspec', '~> 3.0'
|
27
|
+
spec.add_dependency 'will_paginate', '>= 3.1'
|
25
28
|
|
26
29
|
spec.add_development_dependency 'bundler', '~> 1.15'
|
27
30
|
spec.add_development_dependency 'json'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonApiToolbox
|
4
|
+
module Paginable
|
5
|
+
def pagination_meta(collection_resource)
|
6
|
+
{
|
7
|
+
'current-page': collection_resource.current_page,
|
8
|
+
'next-page': collection_resource.next_page,
|
9
|
+
'prev-page': collection_resource.previous_page,
|
10
|
+
'total-pages': collection_resource.total_pages,
|
11
|
+
'total-count': collection_resource.total_entries
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def pagination(collection_resource)
|
16
|
+
{
|
17
|
+
first: 1,
|
18
|
+
last: collection_resource.total_pages,
|
19
|
+
prev: collection_resource.previous_page,
|
20
|
+
next: collection_resource.next_page
|
21
|
+
}.reduce({}, &method(:page_links))
|
22
|
+
end
|
23
|
+
|
24
|
+
def page_links(paginations, entry)
|
25
|
+
link, page = entry
|
26
|
+
if page
|
27
|
+
paginations.merge(create_page_link(link, page))
|
28
|
+
else
|
29
|
+
paginations
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_page_link(link, page)
|
34
|
+
link_path = collection_resource_path(page: page,
|
35
|
+
per_page: params[:per_page])
|
36
|
+
{ link => link_path }
|
37
|
+
end
|
38
|
+
|
39
|
+
def collection_resource_path(options)
|
40
|
+
path_method = "#{controller_name.pluralize.underscore}_path"
|
41
|
+
public_send(path_method, options)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/json_api_toolbox.rb
CHANGED
@@ -5,6 +5,7 @@ require 'renderizable'
|
|
5
5
|
require 'enums'
|
6
6
|
require 'postable'
|
7
7
|
require 'jsonapi/serializable'
|
8
|
+
require 'json_api_toolbox/paginable'
|
8
9
|
|
9
10
|
require_relative 'json_api_toolbox/spec_support/shared_examples_for_controllers'
|
10
11
|
require_relative 'json_api_toolbox/serializables/serializable_enum_option'
|
data/lib/renderizable.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_toolbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adriano Bacha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
13
41
|
- !ruby/object:Gem::Dependency
|
14
42
|
name: activesupport
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +80,20 @@ dependencies:
|
|
52
80
|
- - "~>"
|
53
81
|
- !ruby/object:Gem::Version
|
54
82
|
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: will_paginate
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.1'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.1'
|
55
97
|
- !ruby/object:Gem::Dependency
|
56
98
|
name: bundler
|
57
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -141,6 +183,7 @@ files:
|
|
141
183
|
- json_api_toolbox.gemspec
|
142
184
|
- lib/enums.rb
|
143
185
|
- lib/json_api_toolbox.rb
|
186
|
+
- lib/json_api_toolbox/paginable.rb
|
144
187
|
- lib/json_api_toolbox/serializables/serializable_enum_option.rb
|
145
188
|
- lib/json_api_toolbox/serializables/serializable_job.rb
|
146
189
|
- lib/json_api_toolbox/spec_support/shared_examples_for_controllers.rb
|