aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/routing/inspector.rb
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2012-12-30 15:04:09 +0100
committerYves Senn <yves.senn@gmail.com>2013-01-05 13:59:14 +0100
commitaf5c0fd85fce1adb311083dd1ecf96432ee8caa3 (patch)
tree7f2676d36cbd5847f9cafe98041272efdbad7ca6 /actionpack/lib/action_dispatch/routing/inspector.rb
parent1e1c7afe3fed844e1a11a75e125a0ac7af1d468b (diff)
downloadrails-af5c0fd85fce1adb311083dd1ecf96432ee8caa3.tar.gz
rails-af5c0fd85fce1adb311083dd1ecf96432ee8caa3.tar.bz2
rails-af5c0fd85fce1adb311083dd1ecf96432ee8caa3.zip
split formatting concerns from RoutesInspector
Diffstat (limited to 'actionpack/lib/action_dispatch/routing/inspector.rb')
-rw-r--r--actionpack/lib/action_dispatch/routing/inspector.rb62
1 files changed, 42 insertions, 20 deletions
diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb
index 73b2b4ac1d..7ae1d2a0ee 100644
--- a/actionpack/lib/action_dispatch/routing/inspector.rb
+++ b/actionpack/lib/action_dispatch/routing/inspector.rb
@@ -61,21 +61,33 @@ module ActionDispatch
##
# This class is just used for displaying route information when someone
- # executes `rake routes`. People should not use this class.
+ # executes `rake routes` or looks at the RoutingError page.
+ # People should not use this class.
class RoutesInspector # :nodoc:
- def initialize
- @engines = Hash.new
+ def initialize(routes)
+ @engines = {}
+ @routes = routes
end
- def format(all_routes, filter = nil)
- if filter
- all_routes = all_routes.select{ |route| route.defaults[:controller] == filter }
+ def format(formatter, filter = nil)
+ routes_to_display = filter_routes(filter)
+
+ routes = collect_routes(routes_to_display)
+ formatter.section :application, 'Application routes', routes
+
+ @engines.each do |name, routes|
+ formatter.section :engine, "Routes for #{name}", routes
end
- routes = collect_routes(all_routes)
+ formatter.result
+ end
- formatted_routes(routes) +
- formatted_routes_for_engines
+ def filter_routes(filter)
+ if filter
+ @routes.select { |route| route.defaults[:controller] == filter }
+ else
+ @routes
+ end
end
def collect_routes(routes)
@@ -100,22 +112,32 @@ module ActionDispatch
@engines[name] = collect_routes(routes.routes)
end
end
+ end
- def formatted_routes_for_engines
- @engines.map do |name, routes|
- ["\nRoutes for #{name}:"] + formatted_routes(routes)
- end.flatten
+ class ConsoleFormatter
+ def initialize
+ @buffer = []
end
- def formatted_routes(routes)
- 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
+ def result
+ @buffer.join("\n")
+ end
- routes.map do |r|
- "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
- end
+ def section(type, title, routes)
+ @buffer << "\n#{title}:" unless type == :application
+ @buffer << draw_section(routes)
end
+
+ private
+ def draw_section(routes)
+ 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]}"
+ end
+ end
end
end
end