Skip to content

Commit

Permalink
Fix permit matcher for multiple instances of params
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ari Pollak authored and geoffharcourt committed Nov 21, 2016
1 parent d1e9a41 commit 44c0198
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/shoulda/matchers/action_controller/permit_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 44c0198

Please sign in to comment.