From c6f3c05ddb85fc81e8f1a9d5c68e7cf4b892bd2b Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 28 Jun 2010 00:53:36 +0100 Subject: [PATCH] Remove invalid conditions from route --- actionpack/lib/action_dispatch/routing/route.rb | 9 +++++++++ .../lib/action_dispatch/routing/route_set.rb | 1 + actionpack/test/dispatch/routing_test.rb | 12 ++++++++++++ 3 files changed, 22 insertions(+), 0 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/route.rb b/actionpack/lib/action_dispatch/routing/route.rb index 6f37ead..8bb0b6c 100644 --- a/actionpack/lib/action_dispatch/routing/route.rb +++ b/actionpack/lib/action_dispatch/routing/route.rb @@ -1,6 +1,7 @@ module ActionDispatch module Routing class Route #:nodoc: + cattr_accessor :valid_conditions attr_reader :app, :conditions, :defaults, :name attr_reader :path, :requirements @@ -24,6 +25,9 @@ module ActionDispatch h[k] = Rack::Mount::RegexpWithNamedGroups.new(v) h } + + @conditions.delete_if{ |k,v| k != :path_info && !valid_condition?(k) } + @requirements.delete_if{ |k,v| !valid_condition?(k) } end def verb @@ -50,6 +54,11 @@ module ActionDispatch "%-6s %-40s %s" % [(verb || :any).to_s.upcase, path, requirements.inspect] end end + + private + def valid_condition?(method) + segment_keys.include?(method) || valid_conditions.include?(method) + end end end end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 05200f0..683053e 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -202,6 +202,7 @@ module ActionDispatch self.controller_namespaces = Set.new self.default_url_options = {} self.request_class = request_class + Route.valid_conditions = request_class.public_instance_methods.select{ |m| m != "id" }.map{ |m| m.to_sym } @disable_clear_and_finalize = false clear! diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index cf92b03..0548456 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -330,6 +330,10 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest resources :content + scope :constraints => { :id => /\d+/ } do + get '/tickets', :to => 'tickets#index', :as => :tickets + end + match '/:locale/*file.:format', :to => 'files#show', :file => /path\/to\/existing\/file/ end end @@ -1546,6 +1550,14 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest end end + def test_router_removes_invalid_conditions + with_test_routes do + get '/tickets' + assert_equal 'tickets#index', @response.body + assert_equal '/tickets', tickets_path + end + end + private def with_test_routes yield -- 1.7.1