From 2794f4164fcdba2cfc704851918c6c869215c045 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 4 Dec 2006 00:12:00 +0000 Subject: [PATCH] Dropped the idea of automatically routing :format for the vanilla routes -- that will be a treat for map.resources. Deprecated the name route root as it'll be used as a shortcut for map.connect '' in Rails 2.0 (Rails 1.2). Added map.root as an alias for map.connect '' (Rails 2.0) git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5671 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/routing.rb | 5 +++++ actionpack/test/controller/routing_test.rb | 22 +++++++++++++++++---- railties/configs/routes.rb | 14 ++++++++----- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 072ca4a865..2bf3bc9a75 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added map.root as an alias for map.connect '' [DHH] + * Added Request#format to return the format used for the request as a mime type. If no format is specified, the first Request#accepts type is used. This means you can stop using respond_to for anything else than responses [DHH]. Examples: GET /posts/5.xml | request.format => Mime::XML diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb index 2fe74c5b34..b0d0dbcf57 100644 --- a/actionpack/lib/action_controller/routing.rb +++ b/actionpack/lib/action_controller/routing.rb @@ -975,6 +975,11 @@ module ActionController @set.add_route(path, options) end + # Creates a named route called "root" for matching the root level request. + def root(options = {}) + named_route("root", '', options) + end + def named_route(name, path, options = {}) @set.add_named_route(name, path, options) end diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 18fcef5bc5..9e48b5391d 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -376,7 +376,7 @@ class LegacyRouteSetTests < Test::Unit::TestCase def test_named_url_with_no_action_specified rs.draw do |map| - map.root '', :controller => 'content' + map.home '', :controller => 'content' map.connect ':controller/:action/:id' end @@ -384,14 +384,14 @@ class LegacyRouteSetTests < Test::Unit::TestCase assert_equal '/', rs.generate(:controller => 'content') x = setup_for_named_route.new - assert_equal({:controller => 'content', :action => 'index', :use_route => :root, :only_path => false}, - x.send(:root_url)) + assert_equal({:controller => 'content', :action => 'index', :use_route => :home, :only_path => false}, + x.send(:home_url)) end def test_url_generated_when_forgetting_action [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash| rs.draw do |map| - map.root '', hash + map.home '', hash map.connect ':controller/:action/:id' end assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'}) @@ -1592,6 +1592,20 @@ class RouteSetTest < Test::Unit::TestCase assert_equal "/people/list", url end + def test_root_map + Object.const_set(:PeopleController, Class.new) + + set.draw { |map| map.root :controller => "people" } + + request.path = "" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("people", request.path_parameters[:controller]) + assert_equal("index", request.path_parameters[:action]) + ensure + Object.send(:remove_const, :PeopleController) + end + def test_generate_finds_best_fit set.draw do |map| map.connect "/people", :controller => "people", :action => "index" diff --git a/railties/configs/routes.rb b/railties/configs/routes.rb index 9bbc463769..0eda6de0cc 100644 --- a/railties/configs/routes.rb +++ b/railties/configs/routes.rb @@ -8,16 +8,20 @@ ActionController::Routing::Routes.draw do |map| # Sample of named route: # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase' # This route can be invoked with purchase_url(:id => product.id) + + # Sample resource route (maps HTTP verbs to controller actions automatically): + # map.resources :products - # You can have the root of your site routed by hooking up '' + # Sample resource route with options: + # map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get } + + # You can have the root of your site routed with map.root # -- just remember to delete public/index.html. - # map.connect '', :controller => "welcome" + # map.root :controller => "welcome" - # Allow downloading Web Service WSDL as a file with an extension - # instead of a file named 'wsdl' + # Allow downloading Web Service WSDL as a file with an extension instead of a file named 'wsdl' map.connect ':controller/service.wsdl', :action => 'wsdl' # Install the default route as the lowest priority. - map.connect ':controller/:action/:id.:format' map.connect ':controller/:action/:id' end