From cbe7899f9d5fece4749f75828fd120d67056f356 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 28 Aug 2015 16:23:31 +0200 Subject: [PATCH] revises 877e42e * A string in the example lacked quotes. * The tests asserted stuff about :last_name, whereas test params do not have that key. * But, the first one passed, why? After hitting my head against the wall and doing some obscure rituals realized the new #require had an important typo, wanted to iterate over the array argument (key), but it ran over its own hash keys (method #keys). * Modified the test to prevent the same typo to happen again. * The second test assigned to an unused variable safe_params that has been therefore removed. * Grammar of the second test description. * Since I was on it, reworded both test descriptions. --- .../lib/action_controller/metal/strong_parameters.rb | 4 ++-- actionpack/test/controller/required_params_test.rb | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index 02599a4654..da507ca294 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -256,11 +256,11 @@ def permit! # ActionController::Parameters.new(first_name: 'Gaurish', title: nil).require([:first_name, :title]) # # => ActionController::ParameterMissing: param is missing or the value is empty: title # - # params = ActionController::Parameters.new(first_name: 'Gaurish', title: Mjallo) + # params = ActionController::Parameters.new(first_name: 'Gaurish', title: 'Mjallo') # first_name, title = params.require([:first_name, :title]) # def require(key) - return keys.map { |k| require(k) } if key.is_a?(Array) + return key.map { |k| require(k) } if key.is_a?(Array) value = self[key] if value.present? || value == false value diff --git a/actionpack/test/controller/required_params_test.rb b/actionpack/test/controller/required_params_test.rb index 98b8e44b22..168f64ce41 100644 --- a/actionpack/test/controller/required_params_test.rb +++ b/actionpack/test/controller/required_params_test.rb @@ -49,20 +49,20 @@ class ParametersRequireTest < ActiveSupport::TestCase end end - test "require array of params" do - safe_params = ActionController::Parameters.new(person: {first_name: 'Gaurish', title: 'Mjallo'}) + test "require array when all required params are present" do + safe_params = ActionController::Parameters.new(person: {first_name: 'Gaurish', title: 'Mjallo', city: 'Barcelona'}) .require(:person) - .require([:first_name, :last_name]) + .require([:first_name, :title]) assert_kind_of Array, safe_params assert_equal ['Gaurish', 'Mjallo'], safe_params end - test "require array when it contains a nil values" do + test "require array when a required param is missing" do assert_raises(ActionController::ParameterMissing) do - safe_params = ActionController::Parameters.new(person: {first_name: 'Gaurish', title: nil}) + ActionController::Parameters.new(person: {first_name: 'Gaurish', title: nil}) .require(:person) - .require([:first_name, :last_name]) + .require([:first_name, :title]) end end end -- GitLab