mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #8499 from schneems/schneems/html-route-inspector
Output routes in :html format
This commit is contained in:
commit
ae68fc3864
3 changed files with 51 additions and 9 deletions
|
@ -67,15 +67,19 @@ module ActionDispatch
|
||||||
@engines = Hash.new
|
@engines = Hash.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def format(all_routes, filter = nil)
|
def format(all_routes, filter = nil, format = :txt)
|
||||||
if filter
|
if filter
|
||||||
all_routes = all_routes.select{ |route| route.defaults[:controller] == filter }
|
all_routes = all_routes.select{ |route| route.defaults[:controller] == filter }
|
||||||
end
|
end
|
||||||
|
|
||||||
routes = collect_routes(all_routes)
|
routes = collect_routes(all_routes)
|
||||||
|
|
||||||
formatted_routes(routes) +
|
routes = formatted_routes(routes, format) + formatted_routes_for_engines(format)
|
||||||
formatted_routes_for_engines
|
if format == :html
|
||||||
|
routes.join('')
|
||||||
|
else
|
||||||
|
routes
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def collect_routes(routes)
|
def collect_routes(routes)
|
||||||
|
@ -101,19 +105,32 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def formatted_routes_for_engines
|
def formatted_routes_for_engines(format)
|
||||||
@engines.map do |name, routes|
|
@engines.map do |name, routes|
|
||||||
["\nRoutes for #{name}:"] + formatted_routes(routes)
|
["\nRoutes for #{name}:"] + formatted_routes(routes, format)
|
||||||
end.flatten
|
end.flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
def formatted_routes(routes)
|
def formatted_routes(routes, format)
|
||||||
name_width = routes.map{ |r| r[:name].length }.max
|
name_width = routes.map{ |r| r[:name].length }.max
|
||||||
verb_width = routes.map{ |r| r[:verb].length }.max
|
verb_width = routes.map{ |r| r[:verb].length }.max
|
||||||
path_width = routes.map{ |r| r[:path].length }.max
|
path_width = routes.map{ |r| r[:path].length }.max
|
||||||
|
|
||||||
routes.map do |r|
|
routes.map do |r|
|
||||||
"#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
|
if format == :txt
|
||||||
|
"#{r[:name].rjust(name_width)} " +
|
||||||
|
"#{r[:verb].ljust(verb_width)} " +
|
||||||
|
"#{r[:path].ljust(path_width)} " +
|
||||||
|
"#{r[:reqs]}"
|
||||||
|
elsif format == :html
|
||||||
|
route = r
|
||||||
|
"<tr class='route-row' data-helper='path' #{[:name, :verb, :path, :reqs].each {|key| "data-#{key}='#{route[key]}'"} } >" +
|
||||||
|
"<td class='route-name'>#{route[:name] + "<span class='helper'>_path</span>" if route[:name].present?}</td>" +
|
||||||
|
"<td class='route-verb'>#{route[:verb]}</td>" +
|
||||||
|
"<td class='route-path'>#{route[:path]}</td>" +
|
||||||
|
"<td class='route-reqs'>#{route[:reqs]}</td>" +
|
||||||
|
"</tr>"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,7 +16,7 @@ class Rails::InfoController < ActionController::Base
|
||||||
|
|
||||||
def routes
|
def routes
|
||||||
inspector = ActionDispatch::Routing::RoutesInspector.new
|
inspector = ActionDispatch::Routing::RoutesInspector.new
|
||||||
@info = inspector.format(_routes.routes).join("\n")
|
@info = inspector.format(_routes.routes, nil, :html)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
<style>
|
||||||
|
.route-row td {padding: 0 30px;}
|
||||||
|
.routeTable {margin: 0 auto 0;}
|
||||||
|
</style>
|
||||||
<h2>
|
<h2>
|
||||||
Routes
|
Routes
|
||||||
</h2>
|
</h2>
|
||||||
|
@ -6,4 +10,25 @@
|
||||||
Routes match in priority from top to bottom
|
Routes match in priority from top to bottom
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p><pre><%= @info %></pre></p>
|
<table id='routeTable' class='routeTable'>
|
||||||
|
<th>Helper<br />
|
||||||
|
<%= link_to "Path", "#", 'data-route-helper' => 'path',
|
||||||
|
title: "Returns a relative path (without the http or domain)" %> /
|
||||||
|
<%= link_to "Url", "#", 'data-route-helper' => 'url',
|
||||||
|
title: "Returns an absolute url (with the http and domain)" %>
|
||||||
|
</th>
|
||||||
|
<th>HTTP Verb</th>
|
||||||
|
<th>Path</th>
|
||||||
|
<th>Controller#Action</th>
|
||||||
|
<%= @info.html_safe %>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<script type='text/javascript'>
|
||||||
|
$(document).ready(function (){
|
||||||
|
$("#routeTable [data-route-helper]").on('click', function(){
|
||||||
|
routeHelper = $(this).data("route-helper");
|
||||||
|
$('.route-name span.helper').html("_" + routeHelper);
|
||||||
|
return false;
|
||||||
|
})
|
||||||
|
})
|
||||||
|
</script>
|
Loading…
Reference in a new issue