Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created January 19, 2018 19:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopsoft/79692dcfc7e5a8f82455b85a743f05c1 to your computer and use it in GitHub Desktop.
Save hopsoft/79692dcfc7e5a8f82455b85a743f05c1 to your computer and use it in GitHub Desktop.
Manually & Recursively Filter Rails Params
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