Permalink
Browse files

Merge pull request #15692 from sromano/falseClass

ActionController::Parameters#require now accepts FalseClass values

Conflicts:
	actionpack/test/controller/required_params_test.rb
  • Loading branch information...
matthewd committed Jun 13, 2014
1 parent b9efc57 commit 7545b27e70687d01b224b9c42d01a37a2028a21a
View
@@ -1,3 +1,9 @@
+* ActionController::Parameters#require now accepts `false` values.
+
+ Fixes #15685.
+
+ *Sergio Romano*
+
* With authorization header `Authorization: Token token=`, `authenticate` now
recognize token as nil, instead of "token".
@@ -180,7 +180,12 @@ def permit!
# ActionController::Parameters.new(person: {}).require(:person)
# # => ActionController::ParameterMissing: param not found: person
def require(key)
- self[key].presence || raise(ParameterMissing.new(key))
+ value = self[key]
+ if value.present? || value == false
+ value
+ else
+ raise ParameterMissing.new(key)
+ end
end
# Alias of #require.
@@ -24,4 +24,28 @@ class ActionControllerRequiredParamsTest < ActionController::TestCase
post :create, { book: { name: "Mjallo!" } }
assert_response :ok
end
+
+ test "required parameters with false value will not raise" do
+ post :create, { book: { name: false } }
+ assert_response :ok
+ end
+end
+
+class ParametersRequireTest < ActiveSupport::TestCase
+
+ test "required parameters should accept and return false value" do
+ assert_equal(false, ActionController::Parameters.new(person: false).require(:person))
+ end
+
+ test "required parameters must not be nil" do
+ assert_raises(ActionController::ParameterMissing) do
+ ActionController::Parameters.new(person: nil).require(:person)
+ end
+ end
+
+ test "required parameters must not be empty" do
+ assert_raises(ActionController::ParameterMissing) do
+ ActionController::Parameters.new(person: {}).require(:person)
+ end
+ end
end

0 comments on commit 7545b27

Please sign in to comment.