From 08d7b186fde6ec1e7779349a6ff00e34b30f294d Mon Sep 17 00:00:00 2001 From: schneems Date: Tue, 11 Dec 2012 15:41:49 -0800 Subject: Output routes in :html format By formatting routes for different media (txt/html) we can apply optimizations based on the format. We can include meta-data in the HTML to allow a rich experience while rendering and viewing the routes. This PR shows route helpers as they are used with the `_path` extension, it also has a javascript toggle on the top to switch to `_url`. This way the developer can see the exact named route helper they can use instead of having to modify a base. This is one example of an optimization that could be applied. Eventually we can link out to guides for the different columns to better explain what helper, HTTP Verb, Path, and Controller#action indicate. We could even add a route search box that could allow developers to input a given route and see all of the routes that match it. These are stand alone features and should be delivered separately. --- .../lib/action_dispatch/routing/inspector.rb | 31 +++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index 8d7461ecc3..63d394be75 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -67,15 +67,19 @@ module ActionDispatch @engines = Hash.new end - def format(all_routes, filter = nil) + def format(all_routes, filter = nil, format = :txt) if filter all_routes = all_routes.select{ |route| route.defaults[:controller] == filter } end routes = collect_routes(all_routes) - formatted_routes(routes) + - formatted_routes_for_engines + routes = formatted_routes(routes, format) + formatted_routes_for_engines(format) + if format == :html + routes.join('') + else + routes + end end def collect_routes(routes) @@ -101,19 +105,32 @@ module ActionDispatch end end - def formatted_routes_for_engines + def formatted_routes_for_engines(format) @engines.map do |name, routes| - ["\nRoutes for #{name}:"] + formatted_routes(routes) + ["\nRoutes for #{name}:"] + formatted_routes(routes, format) end.flatten end - def formatted_routes(routes) + def formatted_routes(routes, format) name_width = routes.map{ |r| r[:name].length }.max verb_width = routes.map{ |r| r[:verb].length }.max path_width = routes.map{ |r| r[:path].length }.max 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 + "" + + "#{route[:name] + "_path" if route[:name].present?}" + + "#{route[:verb]}" + + "#{route[:path]}" + + "#{route[:reqs]}" + + "" + end end end end -- cgit v1.2.3