Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make required() a method and not a proxy.

  • Loading branch information...
commit 50ac31a847e31498930608f686b7dc095f33af2c 1 parent 717d6a0
José Valim josevalim authored
8 lib/action_controller/parameters.rb
View
@@ -10,8 +10,12 @@ def initialize(attributes = nil, tainted = true)
taint if tainted
end
- def required
- RequiredParameters.new(self, tainted?)
+ def required(arg=nil)
+ if arg
+ self[arg].presence || raise(ActionController::ParameterMissing)
+ else
+ RequiredParameters.new(self, tainted?)
+ end
end
def permit(*keys)
14 test/action_controller_required_params_test.rb
View
@@ -5,6 +5,11 @@ def create
params.required[:book]
head :ok
end
+
+ def update
+ params.required(:book).required(:name)
+ head :ok
+ end
end
class ActionControllerRequiredParamsTest < ActionController::TestCase
@@ -13,10 +18,19 @@ class ActionControllerRequiredParamsTest < ActionController::TestCase
test "missing required parameters will raise exception" do
post :create, { magazine: { name: "Mjallo!" } }
assert_response :bad_request
+
+ post :update, { magazine: { name: "Mjallo!" } }
+ assert_response :bad_request
+
+ post :update, { book: { unknown: "Mjallo!" } }
+ assert_response :bad_request
end
test "required parameters that are present will not raise" do
post :create, { book: { name: "Mjallo!" } }
assert_response :ok
+
+ post :update, { book: { name: "Mjallo!" } }
+ assert_response :ok
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.