mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Allow to display engine's routes when running rake routes ENGINES=true
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
## 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*
|
||||
|
||||
|
||||
@@ -4,12 +4,23 @@ module Rails
|
||||
# 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 = ActiveSupport::OrderedHash.new
|
||||
end
|
||||
|
||||
def format all_routes, filter = nil
|
||||
if filter
|
||||
all_routes = all_routes.select{ |route| route.defaults[:controller] == filter }
|
||||
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
|
||||
|
||||
rack_app = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
|
||||
@@ -25,12 +36,32 @@ module Rails
|
||||
|
||||
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
|
||||
|
||||
# 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
|
||||
verb_width = routes.map{ |r| r[:verb].length }.max
|
||||
path_width = routes.map{ |r| r[:path].length }.max
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
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
|
||||
Rails.application.reload_routes!
|
||||
all_routes = Rails.application.routes.routes
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require 'test/unit'
|
||||
require 'rails/application/route_inspector'
|
||||
require 'action_controller'
|
||||
require 'rails/engine'
|
||||
|
||||
module ApplicationTests
|
||||
class RouteInspectTest < Test::Unit::TestCase
|
||||
@@ -9,6 +10,35 @@ module ApplicationTests
|
||||
@inspector = Rails::Application::RouteInspector.new
|
||||
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
|
||||
@set.draw do
|
||||
get '/cart', :to => 'cart#show'
|
||||
|
||||
Reference in New Issue
Block a user