Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fork rack build nested query to support to_param

  • Loading branch information...
commit 84be6cfb6452a23d5617cd8a8b200d8fb0431d5a 1 parent de40bc0
@josh josh authored
Showing with 43 additions and 2 deletions.
  1. +43 −2 actionpack/lib/action_dispatch/routing/route_set.rb
View
45 actionpack/lib/action_dispatch/routing/route_set.rb
@@ -379,7 +379,8 @@ def generate(options, recall = {}, method = :generate)
end
recall[:action] = options.delete(:action) if options[:action] == 'index'
- parameterize = lambda { |name, value|
+ opts = {}
+ opts[:parameterize] = lambda { |name, value|
if name == :controller
value
elsif value.is_a?(Array)
@@ -389,7 +390,22 @@ def generate(options, recall = {}, method = :generate)
end
}
- path = @set.url(named_route, options, recall, :parameterize => parameterize)
+ unless result = @set.generate(:path_info, named_route, options, recall, opts)
+ raise ActionController::RoutingError, "No route matches #{options.inspect}"
+ end
+
+ uri, params = result
+ params.each do |k, v|
+ if v
+ params[k] = v
+ else
+ params.delete(k)
+ end
+ end
+
+ uri << "?#{build_nested_query(params)}" if uri && params.any?
+ path = uri
+
if path && method == :generate_extras
uri = URI(path)
extras = uri.query ?
@@ -456,6 +472,31 @@ def recognize_path(path, environment = {}, rescue_error = true)
def extract_request_environment(request)
{ :method => request.method }
end
+
+ private
+ def build_nested_query(value, prefix = nil)
+ case value
+ when Array
+ value.map { |v|
+ build_nested_query(v, "#{prefix}[]")
+ }.join("&")
+ when Hash
+ value.map { |k, v|
+ build_nested_query(v, prefix ? "#{prefix}[#{k}]" : k)
+ }.join("&")
+ when String
+ raise ArgumentError, "value must be a Hash" if prefix.nil?
+ "#{Rack::Utils.escape(prefix)}=#{Rack::Utils.escape(value)}"
+ when NilClass
+ Rack::Utils.escape(prefix)
+ else
+ if value.respond_to?(:to_param)
+ build_nested_query(value.to_param.to_s, prefix)
+ else
+ Rack::Utils.escape(prefix)
+ end
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.