From 44c0198830921650af3b4a56f5d72aaae2168480 Mon Sep 17 00:00:00 2001 From: Ari Pollak Date: Thu, 11 Feb 2016 10:02:45 -0500 Subject: [PATCH] Fix permit matcher for multiple instances of params When the permit matcher is used without `#on`, the controller does not use `params#require`, and the params object is duplicated, the matcher did not recognize the `#permit` call inside the controller. This happened because the matcher overwrote double registries with the same parameter hash whenever ActionController::Parameters was instantiated. This is related to #899. --- lib/shoulda/matchers/action_controller/permit_matcher.rb | 9 ++++----- .../matchers/action_controller/permit_matcher_spec.rb | 9 +++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/shoulda/matchers/action_controller/permit_matcher.rb b/lib/shoulda/matchers/action_controller/permit_matcher.rb index 5cdbb5c36..8e8b5c48f 100644 --- a/lib/shoulda/matchers/action_controller/permit_matcher.rb +++ b/lib/shoulda/matchers/action_controller/permit_matcher.rb @@ -336,7 +336,7 @@ def parameter_names_as_sentence # @private class CompositeParametersDoubleRegistry def initialize - @parameters_double_registries_by_params = {} + @parameters_double_registries = [] end def register @@ -347,20 +347,19 @@ def register params = call.return_value parameters_double_registry = ParametersDoubleRegistry.new(params) parameters_double_registry.register - parameters_double_registries_by_params[params] = - parameters_double_registry + parameters_double_registries << parameters_double_registry end end def permitted_parameter_names(options = {}) - parameters_double_registries_by_params.flat_map do |params, double_registry| + parameters_double_registries.flat_map do |double_registry| double_registry.permitted_parameter_names(options) end end protected - attr_reader :parameters_double_registries_by_params + attr_reader :parameters_double_registries end # @private diff --git a/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb b/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb index ab386859f..1a1a5a55b 100644 --- a/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb @@ -190,6 +190,15 @@ def params_with_conditional_require(params, *filters) for(:update, params: { id: 1 }) end + it 'works when multiple ActionController::Parameters were instantiated' do + define_controller_with_strong_parameters(action: :create) do + params.permit(:name) + params.dup + end + + expect(controller).to permit(:name).for(:create) + end + describe '#matches?' do it 'does not raise an error when #fetch was used instead of #require (issue #495)' do matcher = permit(:eta, :diner_id).for(:create)