Permalink
Browse files

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.
  • Loading branch information...
tenderlove committed Jul 22, 2015
1 parent 3f29929 commit 52cf1a71b393486435fab4386a8663b146608996
@@ -290,15 +290,15 @@ def session_options=(options)
# Override Rack's GET method to support indifferent access
def GET
@env["action_dispatch.request.query_parameters"] ||= Utils.deep_munge(normalize_encode_params(super || {}))
@env["action_dispatch.request.query_parameters"] ||= normalize_encode_params(super || {})
rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError => e
raise ActionController::BadRequest.new(:query, e)
end
alias :query_parameters :GET
# Override Rack's POST method to support indifferent access
def POST
@env["action_dispatch.request.request_parameters"] ||= Utils.deep_munge(normalize_encode_params(super || {}))
@env["action_dispatch.request.request_parameters"] ||= normalize_encode_params(super || {})
rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError => e
raise ActionController::BadRequest.new(:request, e)
end
@@ -318,11 +318,6 @@ def local?
LOCALHOST =~ remote_addr && LOCALHOST =~ remote_ip
end
protected
def parse_query(*)
Utils.deep_munge(super)
end
private
def check_method(name)
HTTP_METHOD_LOOKUP[name] || raise(ActionController::UnknownHttpMethod, "#{name}, accepted HTTP methods are #{HTTP_METHODS[0...-1].join(', ')}, and #{HTTP_METHODS[-1]}")
@@ -17,7 +17,7 @@ def initialize(message, original_exception)
Mime::JSON => lambda { |raw_post|
data = ActiveSupport::JSON.decode(raw_post)
data = {:_json => data} unless data.is_a?(Hash)
Request::Utils.deep_munge(data).with_indifferent_access
Request::Utils.normalize_encode_params(data)
}
}
@@ -6,10 +6,14 @@ class Utils # :nodoc:
self.perform_deep_munge = true
def self.normalize_encode_params(params)
ParamEncoder.normalize_encode_params params
if perform_deep_munge
NoNilParamEncoder.normalize_encode_params params
else
ParamEncoder.normalize_encode_params params
end
end
class ParamEncoder
class ParamEncoder # :nodoc:
# Convert nested Hash to HashWithIndifferentAccess.
#
def self.normalize_encode_params(params)
@@ -34,22 +38,12 @@ def self.handle_array(params)
end
end
class << self
# Remove nils from the params hash
def deep_munge(hash)
return hash unless perform_deep_munge
hash.each do |k, v|
case v
when Array
v.grep(Hash) { |x| deep_munge(x) }
v.compact!
when Hash
deep_munge(v)
end
end
hash
# Remove nils from the params hash
class NoNilParamEncoder < ParamEncoder # :nodoc:
def self.handle_array(params)
list = super
list.compact!
list
end
end
end

0 comments on commit 52cf1a7

Please sign in to comment.