From 95762cbbb3528bcb6a1a8119795df9647daaaab6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 27 Dec 2009 14:13:03 -0800 Subject: Added shorthand for match 'products/overview' that expands to match 'products/overview', :to => 'products#overview', :as => 'products_overview' --- actionpack/lib/action_dispatch/routing/mapper.rb | 16 +++++++++++++++- actionpack/test/dispatch/routing_test.rb | 22 ++++++++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index e655d6a708..3d604158f4 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -43,15 +43,29 @@ module ActionDispatch def extract_path_and_options(args) options = args.extract_options! - if args.empty? + case + when using_to_shorthand?(args, options) path, to = options.find { |name, value| name.is_a?(String) } options.merge!(:to => to).delete(path) if path + when using_match_shorthand?(args, options) + path = args.first + options = { :to => path.gsub("/", "#"), :as => path.gsub("/", "_") } else path = args.first end [ normalize_path(path), options ] end + + # match "account" => "account#index" + def using_to_shorthand?(args, options) + args.empty? && options.present? + end + + # match "account/overview" + def using_match_shorthand?(args, options) + args.present? && options.except(:via).empty? && args.first.exclude?(":") + end def normalize_path(path) path = nil if path == "" diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 360ffe977b..c4b0b9cdbf 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -16,25 +16,27 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest Routes = routes Routes.draw do controller :sessions do - get 'login', :to => :new, :as => :login - post 'login', :to => :create + get 'login' => :new, :as => :login + post 'login' => :create - delete 'logout', :to => :destroy, :as => :logout + delete 'logout' => :destroy, :as => :logout end match 'account/logout' => redirect("/logout"), :as => :logout_redirect match 'account/login', :to => redirect("/login") + match 'account/overview' + match 'account/modulo/:name', :to => redirect("/%{name}s") match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" } match 'openid/login', :via => [:get, :post], :to => "openid#login" controller(:global) do - match 'global/:action' + get 'global/hide_notice' match 'global/export', :to => :export, :as => :export_request - match 'global/hide_notice', :to => :hide_notice, :as => :hide_notice match '/export/:id/:file', :to => :export, :as => :export_download, :constraints => { :file => /.*/ } + match 'global/:action' end constraints(:ip => /192\.168\.1\.\d\d\d/) do @@ -221,7 +223,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest assert_equal 'global#export', @response.body assert_equal '/global/export', export_request_path - assert_equal '/global/hide_notice', hide_notice_path + assert_equal '/global/hide_notice', global_hide_notice_path assert_equal '/export/123/foo.txt', export_download_path(:id => 123, :file => 'foo.txt') end end @@ -486,6 +488,14 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest assert_equal 'projects#info', @response.body end end + + def test_convention_match_with_no_scope + with_test_routes do + assert_equal '/account/overview', account_overview_path + get '/account/overview' + assert_equal 'account#overview', @response.body + end + end private def with_test_routes -- cgit v1.2.3