提交 0cd3bf84 编写于 作者: P Piotr Sarnacki

Allow to display engine's routes when running `rake routes ENGINES=true`

上级 30cf3e16
## Rails 3.2.0 (unreleased) ## ## Rails 3.2.0 (unreleased) ##
* Allow to change the loading order of railties with `config.railties_order=`. Example: * Added displaying of mounted engine's routes with `rake routes ENGINES=true`. *Piotr Sarnacki*
config.railties_order = [Blog::Engine, :main_app, :all] * Allow to change the loading order of railties with `config.railties_order=`. *Piotr Sarnacki*
Example:
config.railties_order = [Blog::Engine, :main_app, :all]
* Scaffold returns 204 No Content for API requests without content. This makes scaffold work with jQuery out of the box. *José Valim* * Scaffold returns 204 No Content for API requests without content. This makes scaffold work with jQuery out of the box. *José Valim*
......
...@@ -4,12 +4,23 @@ class Application ...@@ -4,12 +4,23 @@ class Application
# This class is just used for displaying route information when someone # This class is just used for displaying route information when someone
# executes `rake routes`. People should not use this class. # executes `rake routes`. People should not use this class.
class RouteInspector # :nodoc: class RouteInspector # :nodoc:
def initialize
@engines = ActiveSupport::OrderedHash.new
end
def format all_routes, filter = nil def format all_routes, filter = nil
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 = all_routes.collect do |route| routes = collect_routes(all_routes)
formatted_routes(routes) +
formatted_routes_for_engines
end
def collect_routes(routes)
routes = routes.collect do |route|
route_reqs = route.requirements route_reqs = route.requirements
rack_app = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/ rack_app = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
...@@ -25,12 +36,32 @@ def format all_routes, filter = nil ...@@ -25,12 +36,32 @@ def format all_routes, filter = nil
verb = route.verb.source.gsub(/[$^]/, '') verb = route.verb.source.gsub(/[$^]/, '')
{:name => route.name.to_s, :verb => verb, :path => route.path.spec.to_s, :reqs => reqs} collect_engine_routes(reqs, rack_app)
{:name => route.name.to_s, :verb => verb, :path => route.path.spec.to_s, :reqs => reqs }
end end
# Skip the route if it's internal info route # Skip the route if it's internal info route
routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} } routes.reject { |r| r[:path] =~ %r{/rails/info/properties|^/assets} }
end
def collect_engine_routes(name, rack_app)
return unless rack_app && ENV["ENGINES"] && rack_app.respond_to?(:routes)
return if @engines[name]
routes = 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 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
......
desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.' desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x. Include engine\'s routes with ENGINES=true'
task :routes => :environment do task :routes => :environment do
Rails.application.reload_routes! Rails.application.reload_routes!
all_routes = Rails.application.routes.routes all_routes = Rails.application.routes.routes
......
require 'test/unit' require 'test/unit'
require 'rails/application/route_inspector' require 'rails/application/route_inspector'
require 'action_controller' require 'action_controller'
require 'rails/engine'
module ApplicationTests module ApplicationTests
class RouteInspectTest < Test::Unit::TestCase class RouteInspectTest < Test::Unit::TestCase
...@@ -9,6 +10,35 @@ def setup ...@@ -9,6 +10,35 @@ def setup
@inspector = Rails::Application::RouteInspector.new @inspector = Rails::Application::RouteInspector.new
end end
def test_displaying_routes_for_engines
ENV["ENGINES"] = "true"
engine = Class.new(Rails::Engine) do
def self.to_s
"Blog::Engine"
end
end
engine.routes.draw do
get '/cart', :to => 'cart#show'
end
@set.draw do
get '/custom/assets', :to => 'custom_assets#show'
mount engine => "/blog", :as => "blog"
end
output = @inspector.format @set.routes
expected = [
"custom_assets GET /custom/assets(.:format) custom_assets#show",
" blog /blog Blog::Engine",
"\nRoutes for Blog::Engine:",
"cart GET /cart(.:format) cart#show"
]
assert_equal expected, output
ensure
ENV["ENGINES"] = nil
end
def test_cart_inspect def test_cart_inspect
@set.draw do @set.draw do
get '/cart', :to => 'cart#show' get '/cart', :to => 'cart#show'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册