Permalink
Browse files

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

  • Loading branch information...
1 parent 30cf3e1 commit 0cd3bf84068dd2b2d0bbb26062f2cdc7093a1b04 @drogus drogus committed Nov 24, 2011
View
7 railties/CHANGELOG.md
@@ -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*
View
37 railties/lib/rails/application/route_inspector.rb
@@ -4,12 +4,23 @@ class Application
# 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 @@ def format all_routes, filter = nil
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
View
2 railties/lib/rails/tasks/routes.rake
@@ -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
View
30 railties/test/application/route_inspect_test.rb
@@ -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 @@ def setup
@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'

4 comments on commit 0cd3bf8

@sgruhier

thanks!

@ugisozols

👍

@amatsuda
Ruby on Rails member

❤️ ❤️ ❤️

@bbenezech

❤️ 👍 ❤️ 👍 ❤️

Please sign in to comment.