From 9d2ea7f623711d13cfcf536de61012f4879c344b Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Wed, 17 Feb 2016 14:44:07 +0100 Subject: Tests for AC::Parameters#== --- actionpack/test/controller/parameters/accessors_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'actionpack') diff --git a/actionpack/test/controller/parameters/accessors_test.rb b/actionpack/test/controller/parameters/accessors_test.rb index 08b3d81bf0..7f46c6439c 100644 --- a/actionpack/test/controller/parameters/accessors_test.rb +++ b/actionpack/test/controller/parameters/accessors_test.rb @@ -135,6 +135,12 @@ class ParametersAccessorsTest < ActiveSupport::TestCase assert(params1 == hash1) end + test "equality with simple types works" do + assert(@params != 'Hello') + assert(@params != 42) + assert(@params != false) + end + test "inspect shows both class name and parameters" do assert_equal( '{"age"=>"32", '\ -- cgit v1.2.3 From 08fd9f4f1174ce20f8ecbcda1c2509bb598ce200 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Wed, 17 Feb 2016 14:44:32 +0100 Subject: Fix AC::Parameters#== with other AC::Parameters Creating a protected getter method for `@parameters`. --- .../action_controller/metal/strong_parameters.rb | 7 +++-- .../test/controller/parameters/accessors_test.rb | 33 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index e17189f9f9..3d3015ad07 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -144,11 +144,10 @@ module ActionController end # Returns true if another +Parameters+ object contains the same content and - # permitted flag, or other Hash-like object contains the same content. This - # override is in place so you can perform a comparison with `Hash`. + # permitted flag, or other Hash-like object contains the same content. def ==(other_hash) if other_hash.respond_to?(:permitted?) - super + self.permitted? == other_hash.permitted? && self.parameters == other_hash.parameters else if other_hash.is_a?(Hash) @parameters == other_hash.with_indifferent_access @@ -597,6 +596,8 @@ module ActionController end protected + attr_reader :parameters + def permitted=(new_permitted) @permitted = new_permitted end diff --git a/actionpack/test/controller/parameters/accessors_test.rb b/actionpack/test/controller/parameters/accessors_test.rb index 7f46c6439c..ef8eb5f33e 100644 --- a/actionpack/test/controller/parameters/accessors_test.rb +++ b/actionpack/test/controller/parameters/accessors_test.rb @@ -135,6 +135,39 @@ class ParametersAccessorsTest < ActiveSupport::TestCase assert(params1 == hash1) end + test "is equal to Parameters instance with same params" do + params1 = ActionController::Parameters.new(a: 1, b: 2) + params2 = ActionController::Parameters.new(a: 1, b: 2) + assert(params1 == params2) + end + + test "is equal to Parameters instance with same permitted params" do + params1 = ActionController::Parameters.new(a: 1, b: 2).permit(:a) + params2 = ActionController::Parameters.new(a: 1, b: 2).permit(:a) + assert(params1 == params2) + end + + test "is equal to Parameters instance with same different source params, but same permitted params" do + params1 = ActionController::Parameters.new(a: 1, b: 2).permit(:a) + params2 = ActionController::Parameters.new(a: 1, c: 3).permit(:a) + assert(params1 == params2) + assert(params2 == params1) + end + + test 'is not equal to an unpermitted Parameters instance with same params' do + params1 = ActionController::Parameters.new(a: 1).permit(:a) + params2 = ActionController::Parameters.new(a: 1) + assert(params1 != params2) + assert(params2 != params1) + end + + test "is not equal to Parameters instance with different permitted params" do + params1 = ActionController::Parameters.new(a: 1, b: 2).permit(:a, :b) + params2 = ActionController::Parameters.new(a: 1, b: 2).permit(:a) + assert(params1 != params2) + assert(params2 != params1) + end + test "equality with simple types works" do assert(@params != 'Hello') assert(@params != 42) -- cgit v1.2.3 From 30440363c077dc081f7f14a2a40ca72b0fbaae67 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Wed, 17 Feb 2016 14:48:35 +0100 Subject: Deprecate AC::Parameters#== with a Hash --- .../action_controller/metal/strong_parameters.rb | 23 +++++++++++++--------- .../test/controller/parameters/accessors_test.rb | 6 ++++-- 2 files changed, 18 insertions(+), 11 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index 3d3015ad07..25ec3cf5b6 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -144,16 +144,21 @@ module ActionController end # Returns true if another +Parameters+ object contains the same content and - # permitted flag, or other Hash-like object contains the same content. - def ==(other_hash) - if other_hash.respond_to?(:permitted?) - self.permitted? == other_hash.permitted? && self.parameters == other_hash.parameters + # permitted flag. + def ==(other) + if other.respond_to?(:permitted?) + self.permitted? == other.permitted? && self.parameters == other.parameters + elsif other.is_a?(Hash) + ActiveSupport::Deprecation.warn <<-WARNING.squish + Comparing equality between `ActionController::Parameters` and a + `Hash` is deprecated and will be removed in Rails 5.1. Please only do + comparisons between instances of `ActionController::Parameters`. If + you need to compare to a hash, first convert it using + `ActionController::Parameters#new`. + WARNING + @parameters == other.with_indifferent_access else - if other_hash.is_a?(Hash) - @parameters == other_hash.with_indifferent_access - else - @parameters == other_hash - end + @parameters == other end end diff --git a/actionpack/test/controller/parameters/accessors_test.rb b/actionpack/test/controller/parameters/accessors_test.rb index ef8eb5f33e..4ef5bed30d 100644 --- a/actionpack/test/controller/parameters/accessors_test.rb +++ b/actionpack/test/controller/parameters/accessors_test.rb @@ -129,10 +129,12 @@ class ParametersAccessorsTest < ActiveSupport::TestCase assert_not @params[:person].values_at(:name).first.permitted? end - test "equality with another hash works" do + test "equality with a hash is deprecated" do hash1 = { foo: :bar } params1 = ActionController::Parameters.new(hash1) - assert(params1 == hash1) + assert_deprecated("will be removed in Rails 5.1") do + assert(params1 == hash1) + end end test "is equal to Parameters instance with same params" do -- cgit v1.2.3