1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/lib/action_dispatch/request/utils.rb
Aaron Patterson 52cf1a71b3 rm deep_munge. You will live on in our hearts (and git history)
Now that we have encoding strategies, we can just walk the params hash
once to encode as HWIA, and remove nils.
2015-07-21 18:14:18 -07:00

52 lines
1.3 KiB
Ruby

module ActionDispatch
class Request < Rack::Request
class Utils # :nodoc:
mattr_accessor :perform_deep_munge
self.perform_deep_munge = true
def self.normalize_encode_params(params)
if perform_deep_munge
NoNilParamEncoder.normalize_encode_params params
else
ParamEncoder.normalize_encode_params params
end
end
class ParamEncoder # :nodoc:
# Convert nested Hash to HashWithIndifferentAccess.
#
def self.normalize_encode_params(params)
case params
when Array
handle_array params
when Hash
if params.has_key?(:tempfile)
ActionDispatch::Http::UploadedFile.new(params)
else
params.each_with_object({}) do |(key, val), new_hash|
new_hash[key] = normalize_encode_params(val)
end.with_indifferent_access
end
else
params
end
end
def self.handle_array(params)
params.map! { |el| normalize_encode_params(el) }
end
end
# Remove nils from the params hash
class NoNilParamEncoder < ParamEncoder # :nodoc:
def self.handle_array(params)
list = super
list.compact!
list
end
end
end
end
end