From 08fd9f4f1174ce20f8ecbcda1c2509bb598ce200 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Wed, 17 Feb 2016 14:44:32 +0100 Subject: [PATCH] Fix AC::Parameters#== with other AC::Parameters Creating a protected getter method for `@parameters`. --- .../metal/strong_parameters.rb | 7 ++-- .../controller/parameters/accessors_test.rb | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) 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)