Created
January 19, 2018 19:04
-
-
Save hopsoft/79692dcfc7e5a8f82455b85a743f05c1 to your computer and use it in GitHub Desktop.
Manually & Recursively Filter Rails Params
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActionControllerParameters | |
def filtered | |
filter_hash self.to_unsafe_hash | |
end | |
private | |
def filterer | |
@filterer ||= ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters) | |
end | |
def filter_array(array) | |
array.map do |item| | |
case item | |
when Array then filter_array(item) | |
when Hash then filter_hash(item) | |
else item | |
end | |
end | |
end | |
def filter_hash(hash) | |
hash.each do |key, value| | |
case value | |
when Array then hash[key] = filter_array(value) | |
when Hash then hash[key] = filter_hash(value) | |
end | |
end | |
filterer.filter hash | |
end | |
end | |
::ActionController::Parameters.send :include, ::ActionControllerParameters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment