strong_parameters 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +13 -0
- data/lib/action_controller/parameters.rb +17 -6
- data/lib/strong_parameters/version.rb +1 -1
- data/test/gemfiles/Gemfile.rails-3.0.x +1 -1
- data/test/gemfiles/Gemfile.rails-3.1.x +1 -1
- data/test/gemfiles/Gemfile.rails-3.2.x +1 -1
- data/test/parameters_permit_test.rb +33 -2
- metadata +10 -23
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +0 -88
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 68061cec78793c90c4dffaefad764efbe8358313
|
4
|
+
data.tar.gz: 2078326f601731323e69251b4e24b6e22eea19ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c1f8bf98587698cd24b65cfa12f1aadf2b62ca6d67de48d9c10d254adaf3f3296cbf843d32c462683eb46c1ef4cd4367329d28eec042241545a711143593eb7
|
7
|
+
data.tar.gz: 26e983eb07378e1720ba0cef6e128c52a8dda2ac30ebc7809b9fc12c4531db855bb648d97b645effd8416673cfc7d9544ccea594ef13f2fcd47830314b208f3a
|
data/README.md
CHANGED
@@ -70,6 +70,15 @@ This declaration whitelists the `name`, `emails` and `friends` attributes. It is
|
|
70
70
|
|
71
71
|
Thanks to Nick Kallen for the permit idea!
|
72
72
|
|
73
|
+
## Require Multiple Parameters
|
74
|
+
|
75
|
+
If you want to make sure that multiple keys are present in a params hash, you can call the method twice:
|
76
|
+
|
77
|
+
``` ruby
|
78
|
+
params.require(:token)
|
79
|
+
params.require(:post).permit(:title)
|
80
|
+
```
|
81
|
+
|
73
82
|
## Handling of Unpermitted Keys
|
74
83
|
|
75
84
|
By default parameter keys that are not explicitly permitted will be logged in the development and test environment. In other environments these parameters will simply be filtered out and ignored.
|
@@ -92,6 +101,10 @@ parameters = ActionController::Parameters.new(raw_parameters)
|
|
92
101
|
user = User.create(parameters.permit(:name, :email))
|
93
102
|
```
|
94
103
|
|
104
|
+
## More Examples
|
105
|
+
|
106
|
+
Head over to the [Rails guide about Action Controller](http://guides.rubyonrails.org/action_controller_overview.html#more-examples).
|
107
|
+
|
95
108
|
## Installation
|
96
109
|
|
97
110
|
In Gemfile:
|
@@ -13,7 +13,7 @@ module ActionController
|
|
13
13
|
|
14
14
|
def initialize(param)
|
15
15
|
@param = param
|
16
|
-
super("
|
16
|
+
super("param is missing or the value is empty: #{param}")
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -111,11 +111,18 @@ module ActionController
|
|
111
111
|
private
|
112
112
|
|
113
113
|
def convert_hashes_to_parameters(key, value)
|
114
|
-
|
114
|
+
converted = convert_value_to_parameters(value)
|
115
|
+
self[key] = converted unless converted.equal?(value)
|
116
|
+
converted
|
117
|
+
end
|
118
|
+
|
119
|
+
def convert_value_to_parameters(value)
|
120
|
+
if value.is_a?(Array)
|
121
|
+
value.map { |_| convert_value_to_parameters(_) }
|
122
|
+
elsif value.is_a?(Parameters) || !value.is_a?(Hash)
|
115
123
|
value
|
116
124
|
else
|
117
|
-
|
118
|
-
self[key] = self.class.new(value)
|
125
|
+
self.class.new(value)
|
119
126
|
end
|
120
127
|
end
|
121
128
|
|
@@ -179,7 +186,7 @@ module ActionController
|
|
179
186
|
|
180
187
|
# Slicing filters out non-declared keys.
|
181
188
|
slice(*filter.keys).each do |key, value|
|
182
|
-
|
189
|
+
next unless value
|
183
190
|
|
184
191
|
if filter[key] == []
|
185
192
|
# Declaration {:comment_ids => []}.
|
@@ -202,7 +209,7 @@ module ActionController
|
|
202
209
|
if value.is_a?(Array)
|
203
210
|
value.map { |el| yield el }.compact
|
204
211
|
# fields_for on an array of records uses numeric hash keys.
|
205
|
-
elsif
|
212
|
+
elsif fields_for_style?(value)
|
206
213
|
hash = value.class.new
|
207
214
|
value.each { |k,v| hash[k] = yield(v, k) }
|
208
215
|
hash
|
@@ -211,6 +218,10 @@ module ActionController
|
|
211
218
|
end
|
212
219
|
end
|
213
220
|
|
221
|
+
def fields_for_style?(object)
|
222
|
+
object.is_a?(Hash) && object.all? { |k, v| k =~ /\A-?\d+\z/ && v.is_a?(Hash) }
|
223
|
+
end
|
224
|
+
|
214
225
|
def unpermitted_parameters!(params)
|
215
226
|
return unless self.class.action_on_unpermitted_parameters
|
216
227
|
|
@@ -16,8 +16,8 @@ class NestedParametersTest < ActiveSupport::TestCase
|
|
16
16
|
test 'if nothing is permitted, the hash becomes empty' do
|
17
17
|
params = ActionController::Parameters.new(:id => '1234')
|
18
18
|
permitted = params.permit
|
19
|
-
permitted.permitted?
|
20
|
-
permitted.empty?
|
19
|
+
assert permitted.permitted?
|
20
|
+
assert permitted.empty?
|
21
21
|
end
|
22
22
|
|
23
23
|
# --- key --------------------------------------------------------------------
|
@@ -95,6 +95,15 @@ class NestedParametersTest < ActiveSupport::TestCase
|
|
95
95
|
assert !permitted.has_key?(:id)
|
96
96
|
end
|
97
97
|
|
98
|
+
test 'do not break params filtering on nil values' do
|
99
|
+
params = ActionController::Parameters.new(:a => 1, :b => [1, 2, 3], :c => nil)
|
100
|
+
|
101
|
+
permitted = params.permit(:a, :c => [], :b => [])
|
102
|
+
assert_equal 1, permitted[:a]
|
103
|
+
assert_equal [1, 2, 3], permitted[:b]
|
104
|
+
assert_equal nil, permitted[:c]
|
105
|
+
end
|
106
|
+
|
98
107
|
# --- key to empty array -----------------------------------------------------
|
99
108
|
|
100
109
|
test 'key to empty array: empty arrays pass' do
|
@@ -309,4 +318,26 @@ class NestedParametersTest < ActiveSupport::TestCase
|
|
309
318
|
assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['0'][0]
|
310
319
|
assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['1'][0]
|
311
320
|
end
|
321
|
+
|
322
|
+
test "nested number as key" do
|
323
|
+
params = ActionController::Parameters.new({
|
324
|
+
:product => {
|
325
|
+
:properties => {
|
326
|
+
'0' => "prop0",
|
327
|
+
'1' => "prop1"
|
328
|
+
}
|
329
|
+
}
|
330
|
+
})
|
331
|
+
params = params.require(:product).permit(:properties => ["0"])
|
332
|
+
assert_not_nil params[:properties]["0"]
|
333
|
+
assert_nil params[:properties]["1"]
|
334
|
+
assert_equal "prop0", params[:properties]["0"]
|
335
|
+
end
|
336
|
+
|
337
|
+
test 'hashes in array values get wrapped' do
|
338
|
+
params = ActionController::Parameters.new(foo: [{}, {}])
|
339
|
+
params[:foo].each do |hash|
|
340
|
+
assert !hash.permitted?
|
341
|
+
end
|
342
|
+
end
|
312
343
|
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strong_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- David Heinemeier Hansson
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-12-21 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: actionpack
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: activemodel
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: railties
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ~>
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :runtime
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ~>
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,17 +55,15 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: rake
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - '>='
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: '0'
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - '>='
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '0'
|
78
69
|
description:
|
@@ -98,8 +89,6 @@ files:
|
|
98
89
|
- test/action_controller_tainted_params_test.rb
|
99
90
|
- test/active_model_mass_assignment_taint_protection_test.rb
|
100
91
|
- test/controller_generator_test.rb
|
101
|
-
- test/dummy/db/test.sqlite3
|
102
|
-
- test/dummy/log/test.log
|
103
92
|
- test/gemfiles/Gemfile.rails-3.0.x
|
104
93
|
- test/gemfiles/Gemfile.rails-3.0.x.lock
|
105
94
|
- test/gemfiles/Gemfile.rails-3.1.x
|
@@ -113,35 +102,32 @@ files:
|
|
113
102
|
- test/test_helper.rb
|
114
103
|
homepage: https://github.com/rails/strong_parameters
|
115
104
|
licenses: []
|
105
|
+
metadata: {}
|
116
106
|
post_install_message:
|
117
107
|
rdoc_options: []
|
118
108
|
require_paths:
|
119
109
|
- lib
|
120
110
|
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
111
|
requirements:
|
123
|
-
- -
|
112
|
+
- - '>='
|
124
113
|
- !ruby/object:Gem::Version
|
125
114
|
version: '0'
|
126
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
-
none: false
|
128
116
|
requirements:
|
129
|
-
- -
|
117
|
+
- - '>='
|
130
118
|
- !ruby/object:Gem::Version
|
131
119
|
version: '0'
|
132
120
|
requirements: []
|
133
121
|
rubyforge_project:
|
134
|
-
rubygems_version: 1.
|
122
|
+
rubygems_version: 2.1.11
|
135
123
|
signing_key:
|
136
|
-
specification_version:
|
124
|
+
specification_version: 4
|
137
125
|
summary: Permitted and required parameters for Action Pack
|
138
126
|
test_files:
|
139
127
|
- test/action_controller_required_params_test.rb
|
140
128
|
- test/action_controller_tainted_params_test.rb
|
141
129
|
- test/active_model_mass_assignment_taint_protection_test.rb
|
142
130
|
- test/controller_generator_test.rb
|
143
|
-
- test/dummy/db/test.sqlite3
|
144
|
-
- test/dummy/log/test.log
|
145
131
|
- test/gemfiles/Gemfile.rails-3.0.x
|
146
132
|
- test/gemfiles/Gemfile.rails-3.0.x.lock
|
147
133
|
- test/gemfiles/Gemfile.rails-3.1.x
|
@@ -153,3 +139,4 @@ test_files:
|
|
153
139
|
- test/parameters_taint_test.rb
|
154
140
|
- test/raise_on_unpermitted_params_test.rb
|
155
141
|
- test/test_helper.rb
|
142
|
+
has_rdoc:
|
data/test/dummy/db/test.sqlite3
DELETED
File without changes
|
data/test/dummy/log/test.log
DELETED
@@ -1,88 +0,0 @@
|
|
1
|
-
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
2
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
3
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
4
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
5
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
6
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
7
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
8
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
9
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
10
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
11
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
12
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
13
|
-
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
14
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
15
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
16
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
17
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
18
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
19
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
20
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
21
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
22
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
23
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
24
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
25
|
-
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
26
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
27
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
28
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
29
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
30
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
31
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
32
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
33
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
34
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
35
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
36
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
37
|
-
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
38
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
39
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
40
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
41
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
42
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
43
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
44
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
45
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
46
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
47
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
48
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
49
|
-
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
50
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
51
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
52
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
53
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
54
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
55
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
56
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
57
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
58
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
59
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
60
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
61
|
-
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
62
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
63
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
64
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
65
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
66
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
67
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
68
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
69
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
70
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
71
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
72
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
73
|
-
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
74
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
75
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
76
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
77
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
78
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
79
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
80
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
81
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
82
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
83
|
-
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
84
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
85
|
-
[1m[36m (0.3ms)[0m [1mbegin transaction[0m
|
86
|
-
[1m[35m (0.0ms)[0m rollback transaction
|
87
|
-
[1m[36m (0.3ms)[0m [1mbegin transaction[0m
|
88
|
-
[1m[35m (0.0ms)[0m rollback transaction
|