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.
This commit is contained in:
Ari Pollak 2016-02-11 10:02:45 -05:00 committed by Geoff Harcourt
parent d1e9a41fca
commit 44c0198830
2 changed files with 13 additions and 5 deletions

View File

@ -336,7 +336,7 @@ module Shoulda
# @private
class CompositeParametersDoubleRegistry
def initialize
@parameters_double_registries_by_params = {}
@parameters_double_registries = []
end
def register
@ -347,20 +347,19 @@ module Shoulda
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

View File

@ -190,6 +190,15 @@ describe Shoulda::Matchers::ActionController::PermitMatcher, type: :controller d
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)