Skip to content

Commit

Permalink
Fix AC::Parameters#== with other AC::Parameters
Browse files Browse the repository at this point in the history
Creating a protected getter method for `@parameters`.
  • Loading branch information
bquorning committed Feb 19, 2016
1 parent 9d2ea7f commit 08fd9f4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 4 additions & 3 deletions actionpack/lib/action_controller/metal/strong_parameters.rb
Expand Up @@ -144,11 +144,10 @@ def initialize(parameters = {})
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
Expand Down Expand Up @@ -597,6 +596,8 @@ def method_missing(method_sym, *args, &block)
end

protected
attr_reader :parameters

def permitted=(new_permitted)
@permitted = new_permitted
end
Expand Down
33 changes: 33 additions & 0 deletions actionpack/test/controller/parameters/accessors_test.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 08fd9f4

Please sign in to comment.