From cefd0cb90463982df420aff4937ab8c27b52ab0f Mon Sep 17 00:00:00 2001 From: James Rosen Date: Fri, 13 Jun 2008 12:20:09 -0400 Subject: [PATCH] allowed Array of request methods in routing conditions --- actionpack/lib/action_controller/routing/route.rb | 6 ++++- actionpack/test/controller/routing_test.rb | 25 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_controller/routing/route.rb b/actionpack/lib/action_controller/routing/route.rb index a0d108b..216f5a9 100644 --- a/actionpack/lib/action_controller/routing/route.rb +++ b/actionpack/lib/action_controller/routing/route.rb @@ -92,7 +92,11 @@ module ActionController # recognition, not generation. def recognition_conditions result = ["(match = #{Regexp.new(recognition_pattern).inspect}.match(path))"] - result << "conditions[:method] === env[:method]" if conditions[:method] + if conditions[:method].kind_of?(Array) + result << "conditions[:method].include?(env[:method])" + elsif conditions[:method] + result << "conditions[:method] === env[:method]" + end result end diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 07c13eb..999e1a4 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -1313,6 +1313,31 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do end end end + + def test_recognize_array_of_methods + begin + Object.const_set(:BooksController, Class.new(ActionController::Base)) + rs.draw do |r| + r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] } + r.connect '/match', :controller => 'books', :action => 'not_get_or_post' + end + + @request = ActionController::TestRequest.new + @request.env["REQUEST_METHOD"] = 'POST' + @request.request_uri = "/match" + assert_nothing_raised { rs.recognize(@request) } + assert_equal 'get_or_post', @request.path_parameters[:action] + + # have to recreate or else the RouteSet uses a cached version: + @request = ActionController::TestRequest.new + @request.env["REQUEST_METHOD"] = 'PUT' + @request.request_uri = "/match" + assert_nothing_raised { rs.recognize(@request) } + assert_equal 'not_get_or_post', @request.path_parameters[:action] + ensure + Object.send(:remove_const, :BooksController) rescue nil + end + end def test_subpath_recognized Object.const_set(:SubpathBooksController, Class.new(ActionController::Base)) -- 1.5.4