tidy_strong_params 0.1.0.beta.1 → 0.1.0.beta.2
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/CHANGELOG.md +14 -0
- data/Gemfile.lock +1 -1
- data/README.md +114 -10
- data/lib/tidy_strong_params/controller.rb +22 -11
- data/lib/tidy_strong_params/errors.rb +15 -0
- data/lib/tidy_strong_params/resource.rb +9 -12
- data/lib/tidy_strong_params/strong_params.rb +30 -12
- data/lib/tidy_strong_params/version.rb +1 -1
- data/lib/tidy_strong_params.rb +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6871b1ff85f8012795cfb170012b98497bdbb12a
|
4
|
+
data.tar.gz: befed333aa6186e62a5006c2c1a54cdf783337e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13de4c78bcdf4402d2766c62f4d10be508473e02bb178f9035f8a22464b894724f76114a4c6c8f2ec20ebbaec3392e46d1cad368ffb4659f16d6b9e8d1b7eccf
|
7
|
+
data.tar.gz: 89e05b4adc7a9440cda4b640524619e421d9fdef562cd7929ba43c3a4dfc4586e09e99883f277d83b520595845653c7cbccc82d82d5811a82b9806c0ddab95ef
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
### 0.1.0.beta.2
|
2
|
+
|
3
|
+
*Radical changes to API*
|
4
|
+
|
5
|
+
- Method hook injected into controllers is now always 'tidy_strong_params' and no longer dynamically named based on the controller name.
|
6
|
+
|
7
|
+
- Naming improvements to methods in StrongParams. `attributes` -> `params` & Class method `build_list` -> 'restrict'
|
8
|
+
d45e3a01bea
|
9
|
+
|
10
|
+
*Additions*
|
11
|
+
- Ability to pass in required params (defaults to requiring nesting under resource name)
|
12
|
+
- New `params` method in StrongParams class allows conditional building params whitelist
|
13
|
+
- Option to pass scope to StrongParams class. Useful for things like current_user
|
14
|
+
- Tap method in StrongParams class for further modifications to whitelist before return
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -15,24 +15,128 @@ gem 'tidy_strong_params'
|
|
15
15
|
|
16
16
|
## Usage
|
17
17
|
|
18
|
-
|
18
|
+
#### Overview
|
19
|
+
|
20
|
+
Create a params directory and create a params class inheriting from `TidyStrongParams::StrongParams` for each whitelist
|
21
|
+
|
22
|
+
```
|
23
|
+
class BookStrongParams < TidyStrongParams::StrongParams
|
24
|
+
params :tile,
|
25
|
+
:publisher,
|
26
|
+
:year,
|
27
|
+
authors: %i[first_name last_name]
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
A `tidy_strong_parms` method is available on each controller which returns the whitelisted params defined by its corresponding StrongParms class. eg. BookStrongParams for the books controller
|
32
|
+
|
33
|
+
```
|
34
|
+
Class BooksController < ApplicationController
|
35
|
+
def update
|
36
|
+
@book.update(tidy_strong_params)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
#### `TidyStrongParams::StrongParams`
|
42
|
+
##### `params` (Class method)
|
43
|
+
|
44
|
+
The `params` helper method takes the list of parameters you would usually pass as arguments to `permit()` in your controller.
|
45
|
+
|
46
|
+
##### params (instance method)
|
47
|
+
|
48
|
+
The params class method above is the simplist and cleanest way to pass a list of permitted attributes. However if you require more control eg. (conditionally permit params) then you can define your own `params` instance method which should return an array of permitted attributes. eg.
|
49
|
+
|
50
|
+
```
|
51
|
+
class BookStrongParams < TidyStrongParams::StrongParams
|
52
|
+
def params
|
53
|
+
[
|
54
|
+
:tile,
|
55
|
+
publisher
|
56
|
+
]
|
57
|
+
end
|
58
|
+
|
59
|
+
def publisher
|
60
|
+
:publisher if raw_params[:book][:publisher] == 'Faber&Faber'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
It overrides anything built using the `params` class method
|
66
|
+
|
67
|
+
##### `required` (Class method)
|
68
|
+
By default params are required to be nested under there resource name. eg. for the books controller it would be equivalent of calling `params.require(:books)`. The name of this required parameter can be changed of disabled (pass false) using the `required` class method.
|
69
|
+
|
70
|
+
```
|
71
|
+
class BookStrongParams < TidyStrongParams::StrongParams
|
72
|
+
required :old_books
|
73
|
+
params :tile,
|
74
|
+
:publisher
|
75
|
+
end
|
76
|
+
|
77
|
+
expects `{ old_books: { tile: "", publisher: ''} }`
|
78
|
+
```
|
79
|
+
|
80
|
+
##### `tap_params` (instance method)
|
81
|
+
|
82
|
+
Allows you to tap into and tweak the list of permitted attributes before they are returned. Main difference between this and the params instance method is this method is called after `require` and can be used in conjunction with the `params` class helper.
|
83
|
+
|
84
|
+
```
|
85
|
+
class BookStrongParams < TidyStrongParams::StrongParams
|
86
|
+
params :tile,
|
87
|
+
:publisher
|
88
|
+
|
89
|
+
def tap_params(whitelist)
|
90
|
+
whitelist[:authors] = ['tom', 'dick', 'harry'] unless whitelist[:authors]
|
91
|
+
whitelist[:request_meta] = raw_params[:book].keys.length
|
92
|
+
end
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
|
97
|
+
##### `restrict` (class method)
|
98
|
+
Although a `tidy_strong_params` method is injected into each controller the `TidyStrongParams::StrongParams` class can be used directly, just pass in the required `raw_params` argument to the `restrict` method.
|
99
|
+
Note resource_name is only needed if there is no `required` key declared on the BookStrongParams class. Scope is optional
|
19
100
|
|
20
101
|
```
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
102
|
+
Class BooksController < ApplicationController
|
103
|
+
tidy_params_scope :current_user
|
104
|
+
|
105
|
+
def create
|
106
|
+
Book.create(CreateBookStrongParams.restrict(raw_params: params, scope: current_user))
|
107
|
+
end
|
108
|
+
|
109
|
+
def update
|
110
|
+
@book.update(UpdateBookStrongParams.restrict(raw_params: params, resource_name: 'book'))
|
111
|
+
end
|
26
112
|
end
|
27
113
|
```
|
28
114
|
|
29
|
-
|
115
|
+
|
116
|
+
#### `tidy_params_scope` (Class method added to controllers)
|
117
|
+
|
118
|
+
As scope for TSP can be set at the controller level which is then passed to the `StrongParams` class. Useful for passing things like `current_user`. If the same scope is used everywhere then may best to set `tidy_params_scope` on the `ApplicationController`
|
30
119
|
|
31
120
|
```
|
32
121
|
Class BooksController < ApplicationController
|
33
|
-
|
34
|
-
|
35
|
-
|
122
|
+
tidy_params_scope :current_user
|
123
|
+
|
124
|
+
def update
|
125
|
+
@book.update(tidy_strong_params)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
```
|
129
|
+
Then scope can be assessed as an attribute in the StrongParams class
|
130
|
+
|
131
|
+
```
|
132
|
+
class BookStrongParams < TidyStrongParams::StrongParams
|
133
|
+
params :tile,
|
134
|
+
:publisher
|
135
|
+
|
136
|
+
def tap_params(whitelist)
|
137
|
+
whitelist.delete(:tile) = unless scope.current_user&.admin?
|
138
|
+
whitelist[:user_id] = scope.current_user&.id
|
139
|
+
end
|
36
140
|
end
|
37
141
|
```
|
38
142
|
|
@@ -3,18 +3,29 @@ require 'tidy_strong_params/resource'
|
|
3
3
|
module TidyStrongParams
|
4
4
|
module Controller # :nodoc:
|
5
5
|
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
resource_name: resource.name
|
15
|
-
)
|
16
|
-
end
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :_tidy_params_scope
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def tidy_params_scope(scope)
|
13
|
+
self._tidy_params_scope = scope
|
17
14
|
end
|
18
15
|
end
|
16
|
+
|
17
|
+
def tidy_params_scope
|
18
|
+
send(_tidy_params_scope) if _tidy_params_scope &&
|
19
|
+
respond_to?(_tidy_params_scope, true)
|
20
|
+
end
|
21
|
+
|
22
|
+
define_method(:tidy_params) do
|
23
|
+
resource = Resource.new(controller_class: self.class.name)
|
24
|
+
resource.strong_params_class.restrict(
|
25
|
+
raw_params: params,
|
26
|
+
resource_name: resource.name,
|
27
|
+
scope: tidy_params_scope
|
28
|
+
)
|
29
|
+
end
|
19
30
|
end
|
20
31
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module TidyStrongParams
|
2
|
+
module Errors
|
3
|
+
class StrongParamsClassUndefinedError < StandardError
|
4
|
+
attr_reader :resource_class_name
|
5
|
+
|
6
|
+
def initialize(resource_class_name)
|
7
|
+
@resource_class_name = resource_class_name
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
"A #{resource_class_name} class has not been defined"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -6,28 +6,25 @@ module TidyStrongParams
|
|
6
6
|
self.controller_class = controller_class
|
7
7
|
end
|
8
8
|
|
9
|
-
def self.prams_method_name(*args)
|
10
|
-
new(*args).prams_method_name
|
11
|
-
end
|
12
|
-
|
13
9
|
def name
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
def prams_method_name
|
18
|
-
"#{name}_params"
|
10
|
+
resource.underscore
|
19
11
|
end
|
20
12
|
|
21
13
|
def strong_params_class
|
22
14
|
return @strong_params_class if @strong_params_class
|
23
|
-
klass = "::#{params_class_name}
|
24
|
-
|
15
|
+
klass = "::#{params_class_name}".safe_constantize
|
16
|
+
raise Errors::StrongParamsClassUndefinedError.new(params_class_name) unless klass
|
17
|
+
@strong_params_class = klass
|
25
18
|
end
|
26
19
|
|
27
20
|
private
|
28
21
|
|
29
|
-
def
|
22
|
+
def resource
|
30
23
|
controller_class.remove('Controller').singularize
|
31
24
|
end
|
25
|
+
|
26
|
+
def params_class_name
|
27
|
+
"#{resource}StrongParams"
|
28
|
+
end
|
32
29
|
end
|
33
30
|
end
|
@@ -1,30 +1,48 @@
|
|
1
1
|
module TidyStrongParams
|
2
2
|
class StrongParams # :nodoc:
|
3
|
-
class_attribute :
|
4
|
-
|
5
|
-
|
3
|
+
class_attribute :_params
|
4
|
+
class_attribute :_required
|
5
|
+
self._params = {}
|
6
|
+
self._required = nil
|
6
7
|
|
7
|
-
|
8
|
+
attr_accessor :raw_params, :resource_name, :scope
|
9
|
+
|
10
|
+
def initialize(raw_params:, resource_name: '', scope: nil)
|
8
11
|
self.raw_params = raw_params
|
9
12
|
self.resource_name = resource_name
|
13
|
+
self.scope = scope
|
10
14
|
end
|
11
15
|
|
12
|
-
def self.
|
13
|
-
new(*args).
|
16
|
+
def self.restrict(*args)
|
17
|
+
new(*args).restrict
|
14
18
|
end
|
15
19
|
|
16
20
|
class << self
|
17
|
-
def
|
18
|
-
self.
|
21
|
+
def params(*attrs)
|
22
|
+
self._params = attrs
|
23
|
+
end
|
24
|
+
|
25
|
+
def required(required)
|
26
|
+
self._required = required
|
19
27
|
end
|
20
28
|
end
|
21
29
|
|
22
|
-
def
|
23
|
-
|
30
|
+
def params
|
31
|
+
_params
|
32
|
+
end
|
33
|
+
|
34
|
+
def restrict
|
35
|
+
required_params.permit(params).to_h.tap{ |whitelist| tap_params(whitelist) }
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def required_params
|
41
|
+
return raw_params if _required == false
|
42
|
+
raw_params.require(_required || resource_name)
|
24
43
|
end
|
25
44
|
|
26
|
-
def
|
27
|
-
resource_name
|
45
|
+
def tap_params(whitelist)
|
28
46
|
end
|
29
47
|
end
|
30
48
|
end
|
data/lib/tidy_strong_params.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tidy_strong_params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.beta.
|
4
|
+
version: 0.1.0.beta.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonny Wheeler
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- ".rspec"
|
112
112
|
- ".rubocop.yml"
|
113
113
|
- ".travis.yml"
|
114
|
+
- CHANGELOG.md
|
114
115
|
- CODE_OF_CONDUCT.md
|
115
116
|
- Gemfile
|
116
117
|
- Gemfile.lock
|
@@ -121,6 +122,7 @@ files:
|
|
121
122
|
- bin/setup
|
122
123
|
- lib/tidy_strong_params.rb
|
123
124
|
- lib/tidy_strong_params/controller.rb
|
125
|
+
- lib/tidy_strong_params/errors.rb
|
124
126
|
- lib/tidy_strong_params/railtie.rb
|
125
127
|
- lib/tidy_strong_params/resource.rb
|
126
128
|
- lib/tidy_strong_params/strong_params.rb
|