jsonapi.rb 1.7.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -2
- data/lib/jsonapi/filtering.rb +7 -2
- data/lib/jsonapi/rails.rb +5 -7
- data/lib/jsonapi/version.rb +1 -1
- data/spec/dummy.rb +2 -3
- data/spec/errors_spec.rb +1 -1
- metadata +25 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2192bbffc57d4ecbe419a90e25728951db4464c5f299e8866afefe72fa393cad
|
4
|
+
data.tar.gz: f7f83b43169207e7cf6d06e6e99cc8c8f705de09c4c83ff9dba5238540577a9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 577c2b0d1801ef2191c25925e407449ff12ba95d7fc5c8433fcbb2b20e532a1b7a51a884f147c5042c38ec27ca9eed6567bfd746aad4ad8895b7a3f90e9043b1
|
7
|
+
data.tar.gz: f6ca1bc49fd89c1ea942f3c047234d76c4fa1d7604bd06aa5d67c43dad93603cb19e6c542ec13837d1db5e3fbe923ec4799fd865c96c08822dcab7cb160d7f7e
|
data/README.md
CHANGED
@@ -39,7 +39,7 @@ The available features include:
|
|
39
39
|
[sparse fields](https://jsonapi.org/format/#fetching-sparse-fieldsets))
|
40
40
|
* [filtering](https://jsonapi.org/format/#fetching-filtering) and
|
41
41
|
[sorting](https://jsonapi.org/format/#fetching-sorting) of the data
|
42
|
-
(powered by Ransack)
|
42
|
+
(powered by Ransack, soft-dependency)
|
43
43
|
* [pagination](https://jsonapi.org/format/#fetching-pagination) support
|
44
44
|
|
45
45
|
## But how?
|
@@ -49,6 +49,16 @@ and [Ransack](https://github.com/activerecord-hackery/ransack).
|
|
49
49
|
|
50
50
|
Thanks to everyone who worked on these amazing projects!
|
51
51
|
|
52
|
+
## Sponsors
|
53
|
+
|
54
|
+
I'm grateful for the following companies for supporting this project!
|
55
|
+
|
56
|
+
<p align="center">
|
57
|
+
<a href="https://www.luneteyewear.com"><img src="https://user-images.githubusercontent.com/112147/136836142-2bfba96e-447f-4eb6-b137-2445aee81b37.png"/></a>
|
58
|
+
<a href="https://www.startuplandia.io"><img src="https://user-images.githubusercontent.com/112147/136836147-93f8ab17-2465-4477-a7ab-e38255483c66.png"/></a>
|
59
|
+
</p>
|
60
|
+
|
61
|
+
|
52
62
|
## Installation
|
53
63
|
|
54
64
|
Add this line to your application's Gemfile:
|
@@ -231,6 +241,8 @@ to filter and sort over a collection of records.
|
|
231
241
|
The support is pretty extended and covers also relationships and composite
|
232
242
|
matchers.
|
233
243
|
|
244
|
+
Please add `ransack` to your `Gemfile` in order to benefit from this functionality!
|
245
|
+
|
234
246
|
Here's an example:
|
235
247
|
|
236
248
|
```ruby
|
@@ -314,7 +326,7 @@ If you want to change the default number of items per page or define a custom lo
|
|
314
326
|
```ruby
|
315
327
|
def jsonapi_page_size(pagination_params)
|
316
328
|
per_page = pagination_params[:size].to_f.to_i
|
317
|
-
per_page = 30 if per_page > 30
|
329
|
+
per_page = 30 if per_page > 30 || per_page < 1
|
318
330
|
per_page
|
319
331
|
end
|
320
332
|
```
|
data/lib/jsonapi/filtering.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
begin
|
2
|
+
require 'active_record'
|
3
|
+
require 'ransack'
|
4
|
+
require_relative 'patches'
|
5
|
+
rescue LoadError
|
6
|
+
warn('Install `ransack` gem before using `JSONAPI::Filtering`!')
|
7
|
+
end
|
3
8
|
|
4
9
|
# Filtering and sorting support
|
5
10
|
module JSONAPI
|
data/lib/jsonapi/rails.rb
CHANGED
@@ -47,7 +47,7 @@ module JSONAPI
|
|
47
47
|
) unless resource.is_a?(ActiveModel::Errors)
|
48
48
|
|
49
49
|
errors = []
|
50
|
-
model = resource.instance_variable_get(
|
50
|
+
model = resource.instance_variable_get(:@base)
|
51
51
|
|
52
52
|
if respond_to?(:jsonapi_serializer_class, true)
|
53
53
|
model_serializer = jsonapi_serializer_class(model, false)
|
@@ -56,8 +56,8 @@ module JSONAPI
|
|
56
56
|
end
|
57
57
|
|
58
58
|
details = {}
|
59
|
-
if ::Rails
|
60
|
-
resource.
|
59
|
+
if ::Rails.gem_version >= Gem::Version.new('6.1')
|
60
|
+
resource.each do |error|
|
61
61
|
attr = error.attribute
|
62
62
|
details[attr] ||= []
|
63
63
|
details[attr] << error.detail.merge(message: error.message)
|
@@ -121,15 +121,13 @@ module JSONAPI
|
|
121
121
|
|
122
122
|
# Checks if an object is a collection
|
123
123
|
#
|
124
|
-
#
|
124
|
+
# Basically forwards it to a [JSONAPI::Serializer] as there's no public API
|
125
125
|
#
|
126
126
|
# @param resource [Object] to check
|
127
127
|
# @param force_is_collection [NilClass] flag to overwrite
|
128
128
|
# @return [TrueClass] upon success
|
129
129
|
def self.is_collection?(resource, force_is_collection = nil)
|
130
|
-
|
131
|
-
|
132
|
-
resource.respond_to?(:size) && !resource.respond_to?(:each_pair)
|
130
|
+
JSONAPI::ErrorSerializer.is_collection?(resource, force_is_collection)
|
133
131
|
end
|
134
132
|
|
135
133
|
# Resolves resource serializer class
|
data/lib/jsonapi/version.rb
CHANGED
data/spec/dummy.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'securerandom'
|
2
|
-
require '
|
3
|
-
require 'action_controller/railtie'
|
4
|
-
require 'jsonapi'
|
2
|
+
require 'rails/all'
|
5
3
|
require 'ransack'
|
4
|
+
require 'jsonapi'
|
6
5
|
|
7
6
|
Rails.logger = Logger.new(STDOUT)
|
8
7
|
Rails.logger.level = ENV['LOG_LEVEL'] || Logger::WARN
|
data/spec/errors_spec.rb
CHANGED
@@ -56,7 +56,7 @@ RSpec.describe NotesController, type: :request do
|
|
56
56
|
.to eq(Rack::Utils::HTTP_STATUS_CODES[422])
|
57
57
|
expect(response_json['errors'][0]['source'])
|
58
58
|
.to eq('pointer' => '/data/relationships/user')
|
59
|
-
if Rails
|
59
|
+
if Rails.gem_version >= Gem::Version.new('6.1')
|
60
60
|
expect(response_json['errors'][0]['detail'])
|
61
61
|
.to eq('User must exist')
|
62
62
|
else
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stas Suscov
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jsonapi-serializer
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rack
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -39,13 +39,13 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
|
-
type: :
|
48
|
+
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
@@ -53,7 +53,21 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: ransack
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: railties
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
73
|
- - ">="
|
@@ -67,7 +81,7 @@ dependencies:
|
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
84
|
+
name: activerecord
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - ">="
|
@@ -251,7 +265,7 @@ homepage: https://github.com/stas/jsonapi.rb
|
|
251
265
|
licenses:
|
252
266
|
- MIT
|
253
267
|
metadata: {}
|
254
|
-
post_install_message:
|
268
|
+
post_install_message:
|
255
269
|
rdoc_options: []
|
256
270
|
require_paths:
|
257
271
|
- lib
|
@@ -266,8 +280,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
266
280
|
- !ruby/object:Gem::Version
|
267
281
|
version: '0'
|
268
282
|
requirements: []
|
269
|
-
rubygems_version: 3.
|
270
|
-
signing_key:
|
283
|
+
rubygems_version: 3.3.3
|
284
|
+
signing_key:
|
271
285
|
specification_version: 4
|
272
286
|
summary: So you say you need JSON:API support in your API...
|
273
287
|
test_files: []
|