1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix AC::Parameters#== with other AC::Parameters

Creating a protected getter method for `@parameters`.
This commit is contained in:
Benjamin Quorning 2016-02-17 14:44:32 +01:00
parent 9d2ea7f623
commit 08fd9f4f11
2 changed files with 37 additions and 3 deletions

View file

@ -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

View file

@ -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)