Skip to content

Commit

Permalink
Merge pull request #36370 from ptoomey3/master
Browse files Browse the repository at this point in the history
Add support for Proc based parameter filtering on arrays of values
  • Loading branch information
rafaelfranca committed Jul 23, 2019
1 parent 06401bb commit 5574097
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion activesupport/lib/active_support/parameter_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ def value_for_key(key, value, parents = [], original_params = nil)
elsif value.is_a?(Hash)
value = call(value, parents, original_params)
elsif value.is_a?(Array)
value = value.map { |v| v.is_a?(Hash) ? call(v, parents, original_params) : v }
# If we don't pop the current parent it will be duplicated as we
# process each array value.
parents.pop if deep_regexps
value = value.map { |v| value_for_key(key, v, parents, original_params) }
# Restore the parent stack after processing the array.
parents.push(key) if deep_regexps
elsif blocks.any?
key = key.dup if key.duplicable?
value = value.dup if value.duplicable?
Expand Down
7 changes: 7 additions & 0 deletions activesupport/test/parameter_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ class ParameterFilterTest < ActiveSupport::TestCase
value.replace("world!") if original_params["barg"]["blah"] == "bar" && key == "hello"
}

filter_words << lambda { |key, value|
value.upcase! if key == "array_elements"
}

parameter_filter = ActiveSupport::ParameterFilter.new(filter_words)
before_filter["barg"] = { :bargain => "gain", "blah" => "bar", "bar" => { "bargain" => { "blah" => "foo", "hello" => "world" } } }
after_filter["barg"] = { :bargain => "niag", "blah" => "[FILTERED]", "bar" => { "bargain" => { "blah" => "[FILTERED]", "hello" => "world!" } } }

before_filter["array_elements"] = %w(element1 element2)
after_filter["array_elements"] = %w(ELEMENT1 ELEMENT2)

assert_equal after_filter, parameter_filter.filter(before_filter)
end
end
Expand Down

0 comments on commit 5574097

Please sign in to comment.