strong_parameters 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,7 +25,7 @@ In addition, parameters can be marked as required and flow through a predefined
25
25
  # since you'll be able to reuse the same permit list between create and update. Also, you
26
26
  # can specialize this method with per-user checking of permissible attributes.
27
27
  def person_params
28
- params.required(:person).permit(:name, :age)
28
+ params.require(:person).permit(:name, :age)
29
29
  end
30
30
  end
31
31
 
@@ -26,9 +26,11 @@ module ActionController
26
26
  self
27
27
  end
28
28
 
29
- def required(key)
29
+ def require(key)
30
30
  self[key].presence || raise(ActionController::ParameterMissing.new(key))
31
31
  end
32
+
33
+ alias :required :require
32
34
 
33
35
  def permit(*filters)
34
36
  params = self.class.new
@@ -36,7 +38,7 @@ module ActionController
36
38
  filters.each do |filter|
37
39
  case filter
38
40
  when Symbol then
39
- params[filter] = self[filter]
41
+ params[filter] = self[filter] if has_key?(filter)
40
42
  when Hash then
41
43
  self.slice(*filter.keys).each do |key, value|
42
44
  return unless value
@@ -62,18 +64,22 @@ module ActionController
62
64
  convert_hashes_to_parameters(key, super)
63
65
  end
64
66
 
65
- def fetch(key)
66
- unless block_given? || key?(key)
67
- raise ActionController::ParameterMissing.new(key)
68
- end
69
-
67
+ def fetch(key, *args)
70
68
  convert_hashes_to_parameters(key, super)
69
+ rescue KeyError
70
+ raise ActionController::ParameterMissing.new(key)
71
71
  end
72
72
 
73
73
  def slice(*keys)
74
74
  self.class.new(super)
75
75
  end
76
76
 
77
+ def dup
78
+ super.tap do |duplicate|
79
+ duplicate.instance_variable_set :@permitted, @permitted
80
+ end
81
+ end
82
+
77
83
  private
78
84
  def convert_hashes_to_parameters(key, value)
79
85
  if value.is_a?(Parameters) || !value.is_a?(Hash)
@@ -1,3 +1,3 @@
1
1
  module StrongParameters
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  class BooksController < ActionController::Base
4
4
  def create
5
- params.required(:book).required(:name)
5
+ params.require(:book).require(:name)
6
6
  head :ok
7
7
  end
8
8
  end
@@ -1,13 +1,102 @@
1
+  (0.3ms) begin transaction
2
+ Processing by PeopleController#create as HTML
3
+ Parameters: {"person"=>{"name"=>"Mjallo!"}}
4
+ Rendered text template (0.0ms)
5
+ Completed 200 OK in 7ms (Views: 6.4ms | ActiveRecord: 0.0ms)
6
+  (0.1ms) rollback transaction
7
+  (0.3ms) begin transaction
8
+ Processing by PeopleController#create as HTML
9
+ Parameters: {"person"=>{"name"=>"Mjallo!"}}
10
+ Rendered text template (0.0ms)
11
+ Completed 200 OK in 7ms (Views: 6.4ms | ActiveRecord: 0.0ms)
12
+  (0.1ms) rollback transaction
13
+  (0.0ms) begin transaction
14
+ Processing by PeopleController#create_with_permit as HTML
15
+ Parameters: {"person"=>{"name"=>"Mjallo!"}}
16
+ Completed 200 OK in 0ms (Views: 0.2ms | ActiveRecord: 0.0ms)
17
+  (0.0ms) rollback transaction
18
+  (0.3ms) begin transaction
19
+ Processing by PeopleController#create as HTML
20
+ Parameters: {"user"=>{"name"=>"Mjallo!"}}
21
+ Completed 500 Internal Server Error in 0ms
22
+  (0.1ms) rollback transaction
23
+  (0.0ms) begin transaction
24
+ Processing by PeopleController#create as HTML
25
+ Parameters: {"person"=>{"name"=>"Mjallo!"}}
26
+ Completed 500 Internal Server Error in 8ms
27
+  (0.1ms) rollback transaction
1
28
   (0.2ms) begin transaction
29
+ Processing by PeopleController#create as HTML
30
+ Parameters: {"user"=>{"name"=>"Mjallo!"}}
31
+ Completed 500 Internal Server Error in 0ms
32
+  (0.1ms) rollback transaction
33
+  (0.0ms) begin transaction
34
+ Processing by PeopleController#create as HTML
35
+ Parameters: {"person"=>{"name"=>"Mjallo!"}}
36
+ Completed 200 OK in 0ms (ActiveRecord: 0.0ms)
2
37
   (0.0ms) rollback transaction
38
+  (0.3ms) begin transaction
39
+ Processing by PeopleController#create as HTML
40
+ Parameters: {"user"=>{"name"=>"Mjallo!"}}
41
+ Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms)
42
+  (0.1ms) rollback transaction
43
+  (0.1ms) begin transaction
44
+ Processing by PeopleController#create as HTML
45
+ Parameters: {"person"=>{"name"=>"Mjallo!"}}
46
+ Completed 200 OK in 0ms (ActiveRecord: 0.0ms)
47
+  (0.0ms) rollback transaction
48
+  (0.3ms) begin transaction
49
+ Processing by PeopleController#create as HTML
50
+ Parameters: {"user"=>{"name"=>"Mjallo!"}}
51
+ Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms)
52
+  (0.1ms) rollback transaction
3
53
   (0.0ms) begin transaction
54
+ Processing by PeopleController#create as HTML
55
+ Parameters: {"person"=>{"name"=>"Mjallo!"}}
56
+ Completed 200 OK in 0ms (ActiveRecord: 0.0ms)
57
+  (0.0ms) rollback transaction
58
+  (0.3ms) begin transaction
59
+  (0.0ms) rollback transaction
60
+  (0.2ms) begin transaction
61
+  (0.0ms) rollback transaction
62
+  (0.3ms) begin transaction
63
+  (0.0ms) rollback transaction
64
+  (0.2ms) begin transaction
65
+  (0.0ms) rollback transaction
66
+  (0.3ms) begin transaction
67
+  (0.0ms) rollback transaction
68
+  (0.3ms) begin transaction
69
+  (0.0ms) rollback transaction
70
+  (0.3ms) begin transaction
71
+  (0.0ms) rollback transaction
72
+  (0.3ms) begin transaction
73
+  (0.0ms) rollback transaction
74
+  (0.2ms) begin transaction
75
+  (0.0ms) rollback transaction
76
+  (0.2ms) begin transaction
77
+  (0.0ms) rollback transaction
78
+  (0.3ms) begin transaction
79
+  (0.0ms) rollback transaction
80
+  (0.2ms) begin transaction
81
+  (0.0ms) rollback transaction
82
+  (0.3ms) begin transaction
83
+  (0.0ms) rollback transaction
84
+  (0.3ms) begin transaction
85
+  (0.0ms) rollback transaction
86
+  (0.2ms) begin transaction
4
87
   (0.0ms) rollback transaction
5
88
   (0.0ms) begin transaction
6
89
   (0.0ms) rollback transaction
90
+  (0.3ms) begin transaction
91
+  (0.0ms) rollback transaction
7
92
   (0.0ms) begin transaction
8
93
   (0.0ms) rollback transaction
9
94
   (0.0ms) begin transaction
10
95
   (0.0ms) rollback transaction
96
+  (0.3ms) begin transaction
97
+  (0.0ms) rollback transaction
98
+  (0.0ms) begin transaction
99
+  (0.0ms) rollback transaction
11
100
   (0.0ms) begin transaction
12
101
   (0.0ms) rollback transaction
13
102
   (0.2ms) begin transaction
@@ -16,10 +105,16 @@
16
105
   (0.0ms) rollback transaction
17
106
   (0.0ms) begin transaction
18
107
   (0.0ms) rollback transaction
108
+  (0.3ms) begin transaction
109
+  (0.0ms) rollback transaction
19
110
   (0.0ms) begin transaction
20
111
   (0.0ms) rollback transaction
21
112
   (0.0ms) begin transaction
22
113
   (0.0ms) rollback transaction
114
+  (0.2ms) begin transaction
115
+  (0.0ms) rollback transaction
116
+  (0.0ms) begin transaction
117
+  (0.0ms) rollback transaction
23
118
   (0.0ms) begin transaction
24
119
   (0.0ms) rollback transaction
25
120
   (0.2ms) begin transaction
@@ -28,10 +123,16 @@
28
123
   (0.0ms) rollback transaction
29
124
   (0.0ms) begin transaction
30
125
   (0.0ms) rollback transaction
126
+  (0.3ms) begin transaction
127
+  (0.0ms) rollback transaction
31
128
   (0.0ms) begin transaction
32
129
   (0.0ms) rollback transaction
33
130
   (0.0ms) begin transaction
34
131
   (0.0ms) rollback transaction
132
+  (0.3ms) begin transaction
133
+  (0.0ms) rollback transaction
134
+  (0.0ms) begin transaction
135
+  (0.0ms) rollback transaction
35
136
   (0.0ms) begin transaction
36
137
   (0.0ms) rollback transaction
37
138
   (0.2ms) begin transaction
@@ -40,7 +141,7 @@
40
141
   (0.0ms) rollback transaction
41
142
   (0.0ms) begin transaction
42
143
   (0.0ms) rollback transaction
43
-  (0.0ms) begin transaction
144
+  (0.3ms) begin transaction
44
145
   (0.0ms) rollback transaction
45
146
   (0.0ms) begin transaction
46
147
   (0.0ms) rollback transaction
@@ -52,18 +153,22 @@
52
153
   (0.0ms) rollback transaction
53
154
   (0.0ms) begin transaction
54
155
   (0.0ms) rollback transaction
55
-  (0.0ms) begin transaction
156
+  (0.3ms) begin transaction
56
157
   (0.0ms) rollback transaction
57
158
   (0.0ms) begin transaction
58
159
   (0.0ms) rollback transaction
59
160
   (0.0ms) begin transaction
60
161
   (0.0ms) rollback transaction
61
-  (0.2ms) begin transaction
162
+  (0.3ms) begin transaction
163
+  (0.0ms) rollback transaction
164
+  (0.0ms) begin transaction
62
165
   (0.0ms) rollback transaction
63
166
   (0.0ms) begin transaction
64
167
   (0.0ms) rollback transaction
65
168
   (0.0ms) begin transaction
66
169
   (0.0ms) rollback transaction
170
+  (0.3ms) begin transaction
171
+  (0.0ms) rollback transaction
67
172
   (0.0ms) begin transaction
68
173
   (0.0ms) rollback transaction
69
174
   (0.0ms) begin transaction
@@ -78,11 +183,37 @@
78
183
   (0.0ms) rollback transaction
79
184
   (0.0ms) begin transaction
80
185
   (0.0ms) rollback transaction
186
+  (0.3ms) begin transaction
187
+  (0.0ms) rollback transaction
188
+  (0.0ms) begin transaction
189
+  (0.0ms) rollback transaction
81
190
   (0.0ms) begin transaction
82
191
   (0.0ms) rollback transaction
83
192
   (0.0ms) begin transaction
84
193
   (0.0ms) rollback transaction
85
194
   (0.3ms) begin transaction
195
+  (0.0ms) rollback transaction
196
+  (0.0ms) begin transaction
197
+  (0.0ms) rollback transaction
198
+  (0.0ms) begin transaction
199
+  (0.0ms) rollback transaction
200
+  (0.0ms) begin transaction
201
+  (0.0ms) rollback transaction
202
+  (0.2ms) begin transaction
203
+ Processing by PeopleController#create as HTML
204
+ Parameters: {"user"=>{"name"=>"Mjallo!"}}
205
+ Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms)
206
+  (0.1ms) rollback transaction
207
+  (0.1ms) begin transaction
208
+ Processing by PeopleController#create as HTML
209
+ Parameters: {"person"=>{"name"=>"Mjallo!"}}
210
+ Completed 200 OK in 0ms (ActiveRecord: 0.0ms)
86
211
   (0.0ms) rollback transaction
87
212
   (0.3ms) begin transaction
88
213
   (0.0ms) rollback transaction
214
+  (0.0ms) begin transaction
215
+  (0.0ms) rollback transaction
216
+  (0.0ms) begin transaction
217
+  (0.0ms) rollback transaction
218
+  (0.0ms) begin transaction
219
+  (0.0ms) rollback transaction
@@ -4,7 +4,7 @@ require 'action_controller/parameters'
4
4
  class ParametersRequireTest < ActiveSupport::TestCase
5
5
  test "required parameters must be present not merely not nil" do
6
6
  assert_raises(ActionController::ParameterMissing) do
7
- ActionController::Parameters.new(person: {}).required(:person)
7
+ ActionController::Parameters.new(person: {}).require(:person)
8
8
  end
9
9
  end
10
10
  end
@@ -15,6 +15,13 @@ class ParametersTaintTest < ActiveSupport::TestCase
15
15
  assert_equal :foo, e.param
16
16
  end
17
17
 
18
+ test "fetch doesnt raise ParameterMissing exception if there is a default" do
19
+ assert_nothing_raised do
20
+ assert_equal "monkey", @params.fetch(:foo, "monkey")
21
+ assert_equal "monkey", @params.fetch(:foo) { "monkey" }
22
+ end
23
+ end
24
+
18
25
  test "permitted is sticky on accessors" do
19
26
  assert !@params.slice(:person).permitted?
20
27
  assert !@params[:person][:name].permitted?
@@ -42,4 +49,13 @@ class ParametersTaintTest < ActiveSupport::TestCase
42
49
  assert_equal "Chicago", @params[:person][:hometown]
43
50
  assert_equal "Jonas", @params[:person][:family][:brother]
44
51
  end
52
+
53
+ test "permitting parameters that are not there should not include the keys" do
54
+ assert !@params.permit(:person, :funky).has_key?(:funky)
55
+ end
56
+
57
+ test "permit state is kept on a dup" do
58
+ @params.permit!
59
+ assert_equal @params.permitted?, @params.dup.permitted?
60
+ end
45
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strong_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-22 00:00:00.000000000 Z
12
+ date: 2012-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
16
- requirement: &70365015572120 !ruby/object:Gem::Requirement
16
+ requirement: &70325350860480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70365015572120
24
+ version_requirements: *70325350860480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activemodel
27
- requirement: &70365015570760 !ruby/object:Gem::Requirement
27
+ requirement: &70325350858180 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.2.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70365015570760
35
+ version_requirements: *70325350858180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70365015568980 !ruby/object:Gem::Requirement
38
+ requirement: &70325350848320 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70365015568980
46
+ version_requirements: *70325350848320
47
47
  description:
48
48
  email:
49
49
  - david@heinemeierhansson.com
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 1.8.15
90
+ rubygems_version: 1.8.11
91
91
  signing_key:
92
92
  specification_version: 3
93
93
  summary: Permitted and required parameters for Action Pack