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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33158dfc4e2dca2de6f9456be63d610b7e9fe991af9978e402f4fb83a1ca3f34
4
- data.tar.gz: 2476592685ac57f54da4bfbcee51c9f82a36a3d8071c64fa353d6e1743fa3479
3
+ metadata.gz: cef6f1279f2c02d4fa7c4672b6c74f7920c17875b05d2bad161d0e6c0776ce45
4
+ data.tar.gz: 0ffe8a25b91a4f5df7e58fa2cadc449912f654c3dddb9497403db5e66a8bffae
5
5
  SHA512:
6
- metadata.gz: 738a1e0b568ddccca26617937b45955d373f000fc56c2a38ddeee9d038566548b644fa8f0b97e03cce14d3fe8834f441354f60f326c380c408e6db0bef99bffe
7
- data.tar.gz: 121fe2100e22ab0d0a073a3474d1ba7233bb842fd48c44fa8e97a4f78bc5bf97d8e212ef92106ed0f3f9b0bf37d39346f4ea5045415970f5fd2da9eb3727cf93
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
 
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jsonapi
4
+ class Error < StandardError; end
5
+
6
+ class InvalidAttributeError < Error
7
+ def initialize(message)
8
+ super(message)
9
+ end
10
+ end
11
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'jsonapi/exceptions'
3
4
  require 'jsonapi/scopes/filters'
4
5
  require 'jsonapi/scopes/sorts'
@@ -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
- records = records.public_send(key, value) if @filters.include?(key.to_sym)
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
@@ -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
- allowed_fields = allowed_fields.map(&:to_sym)
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
- order = filtered_fields.presence || default_order
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jsonapi
4
4
  module Scopes
5
- VERSION = '0.2.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  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.2.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-08-01 00:00:00.000000000 Z
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