strong_resources 0.6.3 → 0.6.4

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: 6fa1858d3151a0ccc3873ce7614ad1c739bd1934
4
- data.tar.gz: ae7a5549f4996d14f1d568f05484d87b4b19f208
3
+ metadata.gz: 54ddba9d5512b6392e25f12d0d8e94b63f612aeb
4
+ data.tar.gz: 80626922f97af4ec215dd645faf0d1ecd42bc422
5
5
  SHA512:
6
- metadata.gz: ff142dcd36ffadd091f4f44d86a4592660435e9a5953fa8227e812bf54736059f84f310f1e4097226c3fbdff2a0690132ed321bc3ec907511b78dfe30ee661ef
7
- data.tar.gz: cfba12fff9d98c201c1c16e518335a8428203ba7d4bf597e51e794675f829e6fcb4b1575ba5c107ab3cb7a14f0ff3f550dd78de695c7a19823a4f8d3db31baa1
6
+ metadata.gz: 89340cb17fb8f8b08f2e9d0edbd3c3480787d5c0e5f910f4b02cbfb4f8576f07d64d488b541fcd11b7ad7db70c2ee9a2004d1777cd54504a2197e198840684bf
7
+ data.tar.gz: 31f4bc9bec27ffb25b5f40fa52e3bb14f32141061714a703be5724607562b90e84bd5b1970004ff13b125aae4d5633ad49cd8b47139bbd54e44f07afb651acfa
@@ -6,6 +6,10 @@ require "strong_resources/configuration"
6
6
  require "strong_resources/strong_resource"
7
7
  require "strong_resources/controller/mixin"
8
8
 
9
+ if defined?(JsonapiErrorable)
10
+ require "strong_resources/exception_handler"
11
+ end
12
+
9
13
  module StrongResources
10
14
  class UnregisteredResource < StandardError
11
15
  def initialize(name)
@@ -17,13 +21,20 @@ module StrongResources
17
21
  end
18
22
  end
19
23
 
20
- class UnregisteredParam < StandardError
21
- def initialize(name)
22
- @name = name
24
+ class UnregisteredType < StandardError
25
+ def initialize(type)
26
+ @type = type
23
27
  end
24
28
 
25
29
  def message
26
- "Parameter with name #{@name} not registered"
30
+ <<-MSG
31
+ Type "#{@type}" was not found!
32
+
33
+ This is the right-hand side of your strong_resource attributes.
34
+
35
+ See the list of default types, and directions on how to register custom
36
+ types, here: https://jsonapi-suite.github.io/strong_resources/#default-types
37
+ MSG
27
38
  end
28
39
  end
29
40
 
@@ -42,8 +53,8 @@ module StrongResources
42
53
  end
43
54
 
44
55
  def self.type_for_param(name)
45
- found = config.strong_params[name][:type]
46
- raise UnregisteredParam.new(name) unless found
47
- found
56
+ found = config.strong_params[name]
57
+ raise UnregisteredType.new(name) unless found
58
+ found[:type]
48
59
  end
49
60
  end
@@ -8,6 +8,16 @@ module StrongResources
8
8
  attr_accessor :_strong_resources
9
9
  end
10
10
  end
11
+
12
+ # In cases when the full jsonapi suite is being used, we
13
+ # can provide much better error feedback when parameters are
14
+ # incorrect. Without this handler, errors would be generic
15
+ # 500's with unhelpful statuses, but this will give API consumers
16
+ # better feedback about formatting errors with a 400 response.
17
+ if respond_to?(:register_exception)
18
+ register_exception StrongerParameters::InvalidParameter,
19
+ handler: StrongResources::ExceptionHandler
20
+ end
11
21
  end
12
22
 
13
23
  # TODO: refactor
@@ -53,7 +63,10 @@ module StrongResources
53
63
  end
54
64
 
55
65
  def strong_resource
56
- resource = self.class._strong_resources[action_name.to_sym]
66
+ resources = self.class._strong_resources
67
+ raise RuntimeError, "You need to define the `strong_resource` for #{self.class.name}" if resources.nil?
68
+ resource = resources[action_name.to_sym]
69
+ raise RuntimeError, "Missing `strong_resource` parameters for #{self.class.name}##{action_name}" if resource.nil?
57
70
  _params = params
58
71
  resource.permits(self)
59
72
  end
@@ -0,0 +1,35 @@
1
+ module StrongResources
2
+ class ExceptionHandler < JsonapiErrorable::ExceptionHandler
3
+ SPLIT_REGEX = /^Invalid parameter: ([\w]+)\W(.*)/
4
+
5
+ def error_payload(error)
6
+ message_parse = error.message.match(SPLIT_REGEX)
7
+
8
+ attribute = message_parse[1]
9
+ message = message_parse[2]
10
+ error = {
11
+ code: 'unprocessable_entity',
12
+ status: '400',
13
+ title: 'Malformed Attribute',
14
+ detail: error.message,
15
+ source: { pointer: "/data/attributes/#{attribute}" },
16
+ meta: {
17
+ attribute: attribute,
18
+ message: message
19
+ }
20
+ }
21
+
22
+ {
23
+ "errors" => [error]
24
+ }
25
+ end
26
+
27
+ def status_code(_)
28
+ 400
29
+ end
30
+
31
+ def log?
32
+ false
33
+ end
34
+ end
35
+ end
@@ -93,11 +93,11 @@ module StrongResources
93
93
 
94
94
  def permits(controller)
95
95
  base_permits(self, controller).tap do |permits|
96
+ permits[:relationships] ||= {}
96
97
  self.relations.each_pair do |relation_name, opts|
97
98
  related_resource = opts[:resource]
98
99
  related = related_permits(related_resource, controller)
99
-
100
- permits[:relationships] ||= {}
100
+
101
101
  permits[:relationships][relation_name] = related
102
102
  permits
103
103
  end
@@ -1,3 +1,3 @@
1
1
  module StrongResources
2
- VERSION = "0.6.3"
2
+ VERSION = "0.6.4"
3
3
  end
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency "actionpack", [">= 4.1", "< 6.0"]
24
24
  spec.add_dependency "activesupport", [">= 4.1", "< 6.0"]
25
25
 
26
+ spec.add_development_dependency "jsonapi_errorable", "~> 0.9.0"
26
27
  spec.add_development_dependency "bundler", "~> 1.11"
27
28
  spec.add_development_dependency "rake", "~> 10.0"
28
29
  spec.add_development_dependency "rspec", "~> 3.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strong_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-16 00:00:00.000000000 Z
11
+ date: 2018-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi_compliable
@@ -78,6 +78,20 @@ dependencies:
78
78
  - - "<"
79
79
  - !ruby/object:Gem::Version
80
80
  version: '6.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: jsonapi_errorable
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: 0.9.0
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: 0.9.0
81
95
  - !ruby/object:Gem::Dependency
82
96
  name: bundler
83
97
  requirement: !ruby/object:Gem::Requirement
@@ -172,6 +186,7 @@ files:
172
186
  - lib/strong_resources.rb
173
187
  - lib/strong_resources/configuration.rb
174
188
  - lib/strong_resources/controller/mixin.rb
189
+ - lib/strong_resources/exception_handler.rb
175
190
  - lib/strong_resources/strong_resource.rb
176
191
  - lib/strong_resources/version.rb
177
192
  - strong_resources.gemspec
@@ -195,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
210
  version: '0'
196
211
  requirements: []
197
212
  rubyforge_project:
198
- rubygems_version: 2.5.1
213
+ rubygems_version: 2.6.13
199
214
  signing_key:
200
215
  specification_version: 4
201
216
  summary: Auto-generate swagger docs and strong params