diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2011-11-24 01:45:50 +0100 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2011-11-24 16:16:00 +0100 |
commit | 0cd3bf84068dd2b2d0bbb26062f2cdc7093a1b04 (patch) | |
tree | 13e3b3e3461f7a4221909bb966f0c6104676e853 /railties | |
parent | 30cf3e16a079f833cf396ad9dfaa3a2d4652142d (diff) | |
download | rails-0cd3bf84068dd2b2d0bbb26062f2cdc7093a1b04.tar.gz rails-0cd3bf84068dd2b2d0bbb26062f2cdc7093a1b04.tar.bz2 rails-0cd3bf84068dd2b2d0bbb26062f2cdc7093a1b04.zip |
Allow to display engine's routes when running `rake routes ENGINES=true`
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG.md | 7 | ||||
-rw-r--r-- | railties/lib/rails/application/route_inspector.rb | 37 | ||||
-rw-r--r-- | railties/lib/rails/tasks/routes.rake | 2 | ||||
-rw-r--r-- | railties/test/application/route_inspect_test.rb | 30 |
4 files changed, 70 insertions, 6 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 2d7a1db2be..a7afb72562 100644 --- a/railties/CHANGELOG.md +++ b/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* diff --git a/railties/lib/rails/application/route_inspector.rb b/railties/lib/rails/application/route_inspector.rb index 8252f21aa7..8da7de2584 100644 --- a/railties/lib/rails/application/route_inspector.rb +++ b/railties/lib/rails/application/route_inspector.rb @@ -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 diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index 7dc54144da..df72baef67 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/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 diff --git a/railties/test/application/route_inspect_test.rb b/railties/test/application/route_inspect_test.rb index 78980705ed..130d4e52f8 100644 --- a/railties/test/application/route_inspect_test.rb +++ b/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 @@ 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' |