diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2011-07-16 11:22:58 -0700 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2011-07-16 11:22:58 -0700 |
commit | 296b1eef67d93658951e4d633bf44acca9a8540d (patch) | |
tree | 7e8d65ba506b26d0818280fe35c9b2429ed0ebdf /railties | |
parent | e98d747705b953a60c276a268513e388440772d3 (diff) | |
parent | 7b619f1d7b3d261918592d4a9144d4a5d335d4b0 (diff) | |
download | rails-296b1eef67d93658951e4d633bf44acca9a8540d.tar.gz rails-296b1eef67d93658951e4d633bf44acca9a8540d.tar.bz2 rails-296b1eef67d93658951e4d633bf44acca9a8540d.zip |
Merge pull request #2082 from htanata/rake_routes_formatting
Better formatting of rake routes (issue #1991)
Diffstat (limited to 'railties')
-rw-r--r-- | railties/guides/source/routing.textile | 8 | ||||
-rw-r--r-- | railties/lib/rails/tasks/routes.rake | 12 | ||||
-rw-r--r-- | railties/test/application/rake_test.rb | 88 |
3 files changed, 100 insertions, 8 deletions
diff --git a/railties/guides/source/routing.textile b/railties/guides/source/routing.textile index 1cbc5c8f6e..6a729d9641 100644 --- a/railties/guides/source/routing.textile +++ b/railties/guides/source/routing.textile @@ -823,10 +823,10 @@ If you want a complete list of all of the available routes in your application, For example, here's a small section of the +rake routes+ output for a RESTful route: <pre> - users GET /users {:controller=>"users", :action=>"index"} -formatted_users GET /users.:format {:controller=>"users", :action=>"index"} - POST /users {:controller=>"users", :action=>"create"} - POST /users.:format {:controller=>"users", :action=>"create"} + users GET /users(.:format) users#index + POST /users(.:format) users#create + new_user GET /users/new(.:format) users#new +edit_user GET /users/:id/edit(.:format) users#edit </pre> You may restrict the listing to the routes that map to a particular controller setting the +CONTROLLER+ environment variable: diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index a0c953967c..0c26bcf790 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -10,8 +10,16 @@ task :routes => :environment do routes = all_routes.collect do |route| reqs = route.requirements.dup - reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/ - reqs = reqs.empty? ? "" : reqs.inspect + rack_app = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/ + + endpoint = rack_app ? rack_app.inspect : "#{reqs[:controller]}##{reqs[:action]}" + constraints = reqs.except(:controller, :action) + + reqs = endpoint == '#' ? '' : endpoint + + unless constraints.empty? + reqs = reqs.empty? ? constraints.inspect : "#{reqs} #{constraints.inspect}" + end {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs} end diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 7671c129e9..cc65a674c9 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -91,7 +91,7 @@ module ApplicationTests get '/cart', :to => 'cart#show' end RUBY - assert_match 'cart GET /cart(.:format)', Dir.chdir(app_path){ `rake routes` } + assert_equal "cart GET /cart(.:format) cart#show\n", Dir.chdir(app_path){ `rake routes` } end def test_rake_routes_shows_custom_assets @@ -100,7 +100,91 @@ module ApplicationTests get '/custom/assets', :to => 'custom_assets#show' end RUBY - assert_match 'custom_assets GET /custom/assets(.:format)', Dir.chdir(app_path){ `rake routes` } + assert_equal "custom_assets GET /custom/assets(.:format) custom_assets#show\n", + Dir.chdir(app_path){ `rake routes` } + end + + def test_rake_routes_shows_resources_route + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + resources :articles + end + RUBY + expected = + " articles GET /articles(.:format) articles#index\n" << + " POST /articles(.:format) articles#create\n" << + " new_article GET /articles/new(.:format) articles#new\n" << + "edit_article GET /articles/:id/edit(.:format) articles#edit\n" << + " article GET /articles/:id(.:format) articles#show\n" << + " PUT /articles/:id(.:format) articles#update\n" << + " DELETE /articles/:id(.:format) articles#destroy\n" + assert_equal expected, Dir.chdir(app_path){ `rake routes` } + end + + def test_rake_routes_shows_root_route + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + root :to => 'pages#main' + end + RUBY + assert_equal "root / pages#main\n", Dir.chdir(app_path){ `rake routes` } + end + + def test_rake_routes_shows_controller_and_action_only_route + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + match ':controller/:action' + end + RUBY + assert_equal " /:controller/:action(.:format) \n", Dir.chdir(app_path){ `rake routes` } + end + + def test_rake_routes_shows_controller_and_action_route_with_constraints + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + match ':controller(/:action(/:id))', :id => /\\d+/ + end + RUBY + assert_equal " /:controller(/:action(/:id))(.:format) {:id=>/\\d+/}\n", Dir.chdir(app_path){ `rake routes` } + end + + def test_rake_routes_shows_route_with_defaults + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + match 'photos/:id' => 'photos#show', :defaults => {:format => 'jpg'} + end + RUBY + assert_equal %Q[ /photos/:id(.:format) photos#show {:format=>"jpg"}\n], Dir.chdir(app_path){ `rake routes` } + end + + def test_rake_routes_shows_route_with_constraints + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + match 'photos/:id' => 'photos#show', :id => /[A-Z]\\d{5}/ + end + RUBY + assert_equal " /photos/:id(.:format) photos#show {:id=>/[A-Z]\\d{5}/}\n", Dir.chdir(app_path){ `rake routes` } + end + + def test_rake_routes_shows_route_with_rack_app + app_file "lib/rack_app.rb", <<-RUBY + class RackApp + class << self + def call(env) + end + end + end + RUBY + + app_file "config/routes.rb", <<-RUBY + require 'rack_app' + + AppTemplate::Application.routes.draw do + match 'foo/:id' => RackApp, :id => /[A-Z]\\d{5}/ + end + RUBY + + assert_equal " /foo/:id(.:format) RackApp {:id=>/[A-Z]\\d{5}/}\n", Dir.chdir(app_path){ `rake routes` } end def test_logger_is_flushed_when_exiting_production_rake_tasks |