jsonapi-scopes 0.2.0 → 0.3.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/README.md +19 -0
- data/lib/jsonapi/exceptions.rb +11 -0
- data/lib/jsonapi/scopes.rb +1 -0
- data/lib/jsonapi/scopes/filters.rb +3 -1
- data/lib/jsonapi/scopes/sorts.rb +7 -8
- data/lib/jsonapi/scopes/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cef6f1279f2c02d4fa7c4672b6c74f7920c17875b05d2bad161d0e6c0776ce45
|
4
|
+
data.tar.gz: 0ffe8a25b91a4f5df7e58fa2cadc449912f654c3dddb9497403db5e66a8bffae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b91918c357e2745abd0813d9b5ce181e8fd9e7262d000591fe5910cf2c3d97942ddc65fc659b00120abd89246d1e3b2e2075fe66d85091d0477fe5ea7617fd45
|
7
|
+
data.tar.gz: c6771c3aa0d76a0b4f176df986ca3bd556f6417723f6af5d6fcbc037415fce66333b8d56d163662c3093bbee5b70d42ea3e4b72b29acea29d4b03a30e06ae3d9
|
data/README.md
CHANGED
@@ -95,6 +95,25 @@ Or use negative sort `/contacts?sort=-firstname` to sort by firstname in `desc`
|
|
95
95
|
|
96
96
|
You can even combine multiple sort `/contacts?sort=lastname,-firstname`
|
97
97
|
|
98
|
+
|
99
|
+
### Rescuing a Bad Request in Rails
|
100
|
+
|
101
|
+
Jsonapi::scope raises a `Jsonapi::InvalidAttributeError` you can [rescue_from](https://guides.rubyonrails.org/action_controller_overview.html#rescue-from) in your `ApplicationController`.
|
102
|
+
|
103
|
+
If you want to follow the specification, you **must** respond with a `400 Bad Request`.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class ApplicationController < ActionController::Base
|
107
|
+
rescue_from Jsonapi::InvalidAttributeError, with: :json_api_bad_request
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def json_api_bad_request(exception)
|
112
|
+
render json: { error: exception.message }, status: :bad_request
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
98
117
|
## Contributing
|
99
118
|
Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.
|
100
119
|
|
data/lib/jsonapi/scopes.rb
CHANGED
@@ -21,7 +21,9 @@ module Jsonapi
|
|
21
21
|
filtering_params.each do |key, value|
|
22
22
|
value = value.to_s.split(',').reject(&:blank?) if value.include?(',')
|
23
23
|
|
24
|
-
|
24
|
+
raise InvalidAttributeError, "#{key} is not valid as filter attribute." unless @filters.include?(key.to_sym)
|
25
|
+
|
26
|
+
records = records.public_send(key, value)
|
25
27
|
end
|
26
28
|
|
27
29
|
records
|
data/lib/jsonapi/scopes/sorts.rb
CHANGED
@@ -21,16 +21,15 @@ module Jsonapi
|
|
21
21
|
def apply_sort(params = {}, options = { allowed: [], default: {} })
|
22
22
|
fields = params.dig(:sort)
|
23
23
|
|
24
|
-
allowed_fields = Array.wrap(options[:allowed]).presence || @sortable_fields
|
25
|
-
|
26
|
-
|
27
|
-
default_order = options[:default].presence || @default_sort
|
28
|
-
default_order = default_order.transform_keys(&:to_sym)
|
29
|
-
|
24
|
+
allowed_fields = (Array.wrap(options[:allowed]).presence || @sortable_fields).map(&:to_sym)
|
25
|
+
default_order = (options[:default].presence || @default_sort).transform_keys(&:to_sym)
|
30
26
|
ordered_fields = convert_to_ordered_hash(fields)
|
31
|
-
filtered_fields = ordered_fields.select { |key, _| allowed_fields.include?(key) }
|
32
27
|
|
33
|
-
|
28
|
+
ordered_fields.each do |field, _|
|
29
|
+
raise InvalidAttributeError, "#{field} is not valid as sort attribute." unless allowed_fields.include?(field)
|
30
|
+
end
|
31
|
+
|
32
|
+
order = ordered_fields.presence || default_order
|
34
33
|
|
35
34
|
self.order(order)
|
36
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guillaume Briday
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- LICENSE
|
49
49
|
- README.md
|
50
50
|
- Rakefile
|
51
|
+
- lib/jsonapi/exceptions.rb
|
51
52
|
- lib/jsonapi/scopes.rb
|
52
53
|
- lib/jsonapi/scopes/filters.rb
|
53
54
|
- lib/jsonapi/scopes/sorts.rb
|