2010-01-16 07:17:03 -05:00
|
|
|
require 'active_support/core_ext/hash/keys'
|
2010-04-21 00:58:58 -04:00
|
|
|
require 'active_support/core_ext/hash/indifferent_access'
|
2010-01-16 07:17:03 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
module Http
|
|
|
|
module Parameters
|
2012-08-09 14:21:58 -04:00
|
|
|
def initialize(env)
|
|
|
|
super
|
|
|
|
@symbolized_path_params = nil
|
|
|
|
end
|
|
|
|
|
2010-01-16 07:17:03 -05:00
|
|
|
# Returns both GET and POST \parameters in a single hash.
|
|
|
|
def parameters
|
2010-06-28 00:12:10 -04:00
|
|
|
@env["action_dispatch.request.parameters"] ||= begin
|
|
|
|
params = request_parameters.merge(query_parameters)
|
|
|
|
params.merge!(path_parameters)
|
|
|
|
encode_params(params).with_indifferent_access
|
|
|
|
end
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
alias :params :parameters
|
|
|
|
|
|
|
|
def path_parameters=(parameters) #:nodoc:
|
2010-08-24 10:05:26 -04:00
|
|
|
@symbolized_path_params = nil
|
2010-01-16 07:17:03 -05:00
|
|
|
@env.delete("action_dispatch.request.parameters")
|
|
|
|
@env["action_dispatch.request.path_parameters"] = parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
# The same as <tt>path_parameters</tt> with explicitly symbolized keys.
|
|
|
|
def symbolized_path_parameters
|
2010-08-24 10:05:26 -04:00
|
|
|
@symbolized_path_params ||= path_parameters.symbolize_keys
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a hash with the \parameters used to form the \path of the request.
|
|
|
|
# Returned hash keys are strings:
|
|
|
|
#
|
|
|
|
# {'action' => 'my_action', 'controller' => 'my_controller'}
|
|
|
|
#
|
|
|
|
# See <tt>symbolized_path_parameters</tt> for symbolized keys.
|
|
|
|
def path_parameters
|
|
|
|
@env["action_dispatch.request.path_parameters"] ||= {}
|
|
|
|
end
|
|
|
|
|
2012-05-02 18:42:43 -04:00
|
|
|
def reset_parameters #:nodoc:
|
|
|
|
@env.delete("action_dispatch.request.parameters")
|
|
|
|
end
|
|
|
|
|
2010-01-20 10:55:33 -05:00
|
|
|
private
|
2010-06-28 00:12:10 -04:00
|
|
|
|
|
|
|
# TODO: Validate that the characters are UTF-8. If they aren't,
|
|
|
|
# you'll get a weird error down the road, but our form handling
|
|
|
|
# should really prevent that from happening
|
|
|
|
def encode_params(params)
|
|
|
|
if params.is_a?(String)
|
|
|
|
return params.force_encoding("UTF-8").encode!
|
|
|
|
elsif !params.is_a?(Hash)
|
|
|
|
return params
|
|
|
|
end
|
|
|
|
|
|
|
|
params.each do |k, v|
|
|
|
|
case v
|
|
|
|
when Hash
|
|
|
|
encode_params(v)
|
|
|
|
when Array
|
|
|
|
v.map! {|el| encode_params(el) }
|
|
|
|
else
|
|
|
|
encode_params(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-11 06:15:34 -04:00
|
|
|
# Convert nested Hash to HashWithIndifferentAccess
|
2010-01-16 07:17:03 -05:00
|
|
|
def normalize_parameters(value)
|
|
|
|
case value
|
|
|
|
when Hash
|
|
|
|
h = {}
|
|
|
|
value.each { |k, v| h[k] = normalize_parameters(v) }
|
|
|
|
h.with_indifferent_access
|
|
|
|
when Array
|
|
|
|
value.map { |e| normalize_parameters(e) }
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-04-21 00:58:58 -04:00
|
|
|
end
|