From dbc43bcce69aa4aaf0ae309bf410e2b786c4e3f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 29 Jun 2012 14:04:18 +0200 Subject: Rename RouteInspect to RoutesInspector for consistency --- railties/lib/rails/application/route_inspector.rb | 121 --------------------- railties/lib/rails/application/routes_inspector.rb | 121 +++++++++++++++++++++ railties/lib/rails/info_controller.rb | 4 +- railties/lib/rails/tasks/routes.rake | 4 +- 4 files changed, 125 insertions(+), 125 deletions(-) delete mode 100644 railties/lib/rails/application/route_inspector.rb create mode 100644 railties/lib/rails/application/routes_inspector.rb (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/application/route_inspector.rb b/railties/lib/rails/application/route_inspector.rb deleted file mode 100644 index 942c4f4789..0000000000 --- a/railties/lib/rails/application/route_inspector.rb +++ /dev/null @@ -1,121 +0,0 @@ -require 'delegate' - -module Rails - class Application - class RouteWrapper < SimpleDelegator - def endpoint - rack_app ? rack_app.inspect : "#{controller}##{action}" - end - - def constraints - requirements.except(:controller, :action) - end - - def rack_app(app = self.app) - @rack_app ||= begin - class_name = app.class.name.to_s - if class_name == "ActionDispatch::Routing::Mapper::Constraints" - rack_app(app.app) - elsif ActionDispatch::Routing::Redirect === app || class_name !~ /^ActionDispatch::Routing/ - app - end - end - end - - def verb - super.source.gsub(/[$^]/, '') - end - - def path - super.spec.to_s - end - - def name - super.to_s - end - - def reqs - @reqs ||= begin - reqs = endpoint - reqs += " #{constraints.inspect}" unless constraints.empty? - reqs - end - end - - def controller - requirements[:controller] || ':controller' - end - - def action - requirements[:action] || ':action' - end - - def internal? - path =~ %r{/rails/info.*|^#{Rails.application.config.assets.prefix}} - end - - def engine? - rack_app && rack_app.respond_to?(:routes) - end - end - - ## - # This class is just used for displaying route information when someone - # executes `rake routes`. People should not use this class. - class RouteInspector # :nodoc: - def initialize - @engines = Hash.new - end - - def format(all_routes, filter = nil) - 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 - end - - def collect_routes(routes) - routes = routes.collect do |route| - RouteWrapper.new(route) - end.reject do |route| - route.internal? - end.collect do |route| - collect_engine_routes(route) - - {:name => route.name, :verb => route.verb, :path => route.path, :reqs => route.reqs } - end - end - - def collect_engine_routes(route) - name = route.endpoint - return unless route.engine? - return if @engines[name] - - routes = route.rack_app.routes - if routes.is_a?(ActionDispatch::Routing::RouteSet) - @engines[name] = collect_routes(routes.routes) - end - end - - def formatted_routes_for_engines - @engines.map do |name, routes| - ["\nRoutes for #{name}:"] + formatted_routes(routes) - end.flatten - 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 - - 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 diff --git a/railties/lib/rails/application/routes_inspector.rb b/railties/lib/rails/application/routes_inspector.rb new file mode 100644 index 0000000000..6b2caf8277 --- /dev/null +++ b/railties/lib/rails/application/routes_inspector.rb @@ -0,0 +1,121 @@ +require 'delegate' + +module Rails + class Application + class RouteWrapper < SimpleDelegator + def endpoint + rack_app ? rack_app.inspect : "#{controller}##{action}" + end + + def constraints + requirements.except(:controller, :action) + end + + def rack_app(app = self.app) + @rack_app ||= begin + class_name = app.class.name.to_s + if class_name == "ActionDispatch::Routing::Mapper::Constraints" + rack_app(app.app) + elsif ActionDispatch::Routing::Redirect === app || class_name !~ /^ActionDispatch::Routing/ + app + end + end + end + + def verb + super.source.gsub(/[$^]/, '') + end + + def path + super.spec.to_s + end + + def name + super.to_s + end + + def reqs + @reqs ||= begin + reqs = endpoint + reqs += " #{constraints.inspect}" unless constraints.empty? + reqs + end + end + + def controller + requirements[:controller] || ':controller' + end + + def action + requirements[:action] || ':action' + end + + def internal? + path =~ %r{/rails/info.*|^#{Rails.application.config.assets.prefix}} + end + + def engine? + rack_app && rack_app.respond_to?(:routes) + end + end + + ## + # This class is just used for displaying route information when someone + # executes `rake routes`. People should not use this class. + class RoutesInspector # :nodoc: + def initialize + @engines = Hash.new + end + + def format(all_routes, filter = nil) + 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 + end + + def collect_routes(routes) + routes = routes.collect do |route| + RouteWrapper.new(route) + end.reject do |route| + route.internal? + end.collect do |route| + collect_engine_routes(route) + + {:name => route.name, :verb => route.verb, :path => route.path, :reqs => route.reqs } + end + end + + def collect_engine_routes(route) + name = route.endpoint + return unless route.engine? + return if @engines[name] + + routes = route.rack_app.routes + if routes.is_a?(ActionDispatch::Routing::RouteSet) + @engines[name] = collect_routes(routes.routes) + end + end + + def formatted_routes_for_engines + @engines.map do |name, routes| + ["\nRoutes for #{name}:"] + formatted_routes(routes) + end.flatten + 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 + + 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 diff --git a/railties/lib/rails/info_controller.rb b/railties/lib/rails/info_controller.rb index 5081074395..bacdcbf3aa 100644 --- a/railties/lib/rails/info_controller.rb +++ b/railties/lib/rails/info_controller.rb @@ -1,4 +1,4 @@ -require 'rails/application/route_inspector' +require 'rails/application/routes_inspector' class Rails::InfoController < ActionController::Base self.view_paths = File.join(File.dirname(__FILE__), 'templates') @@ -15,7 +15,7 @@ class Rails::InfoController < ActionController::Base end def routes - inspector = Rails::Application::RouteInspector.new + inspector = Rails::Application::RoutesInspector.new @info = inspector.format(_routes.routes).join("\n") end diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index 5778b22f18..4ade825616 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -1,7 +1,7 @@ desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.' task :routes => :environment do all_routes = Rails.application.routes.routes - require 'rails/application/route_inspector' - inspector = Rails::Application::RouteInspector.new + require 'rails/application/routes_inspector' + inspector = Rails::Application::RoutesInspector.new puts inspector.format(all_routes, ENV['CONTROLLER']).join "\n" end -- cgit v1.2.3