Skip to content
This repository
Browse code

Remove invalid conditions from route [#4989 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit 19ccd4628c1ab4aebc00dc8d480d6cbf1688a312 1 parent 6d04fa6
Andrew White authored josevalim committed
13  actionpack/lib/action_dispatch/routing/route.rb
@@ -2,9 +2,10 @@ module ActionDispatch
2 2
   module Routing
3 3
     class Route #:nodoc:
4 4
       attr_reader :app, :conditions, :defaults, :name
5  
-      attr_reader :path, :requirements
  5
+      attr_reader :path, :requirements, :set
6 6
 
7  
-      def initialize(app, conditions, requirements, defaults, name, anchor)
  7
+      def initialize(set, app, conditions, requirements, defaults, name, anchor)
  8
+        @set = set
8 9
         @app = app
9 10
         @defaults = defaults
10 11
         @name = name
@@ -24,6 +25,9 @@ def initialize(app, conditions, requirements, defaults, name, anchor)
24 25
           h[k] = Rack::Mount::RegexpWithNamedGroups.new(v)
25 26
           h
26 27
         }
  28
+
  29
+        @conditions.delete_if{ |k,v| k != :path_info && !valid_condition?(k) }
  30
+        @requirements.delete_if{ |k,v| !valid_condition?(k) }
27 31
       end
28 32
 
29 33
       def verb
@@ -50,6 +54,11 @@ def to_s
50 54
           "%-6s %-40s %s" % [(verb || :any).to_s.upcase, path, requirements.inspect]
51 55
         end
52 56
       end
  57
+
  58
+      private
  59
+        def valid_condition?(method)
  60
+          segment_keys.include?(method) || set.valid_conditions.include?(method)
  61
+        end
53 62
     end
54 63
   end
55 64
 end
5  actionpack/lib/action_dispatch/routing/route_set.rb
@@ -189,7 +189,7 @@ def #{selector}(*args)
189 189
 
190 190
       attr_accessor :set, :routes, :named_routes
191 191
       attr_accessor :disable_clear_and_finalize, :resources_path_names
192  
-      attr_accessor :default_url_options, :request_class
  192
+      attr_accessor :default_url_options, :request_class, :valid_conditions
193 193
 
194 194
       def self.default_resources_path_names
195 195
         { :new => 'new', :edit => 'edit' }
@@ -202,6 +202,7 @@ def initialize(request_class = ActionDispatch::Request)
202 202
         self.controller_namespaces = Set.new
203 203
         self.default_url_options = {}
204 204
         self.request_class = request_class
  205
+        self.valid_conditions = request_class.public_instance_methods.select{ |m| m != "id" }.map{ |m| m.to_sym }
205 206
 
206 207
         @disable_clear_and_finalize = false
207 208
         clear!
@@ -279,7 +280,7 @@ def empty?
279 280
       end
280 281
 
281 282
       def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
282  
-        route = Route.new(app, conditions, requirements, defaults, name, anchor)
  283
+        route = Route.new(self, app, conditions, requirements, defaults, name, anchor)
283 284
         @set.add_route(*route)
284 285
         named_routes[name] = route if name
285 286
         routes << route
12  actionpack/test/dispatch/routing_test.rb
@@ -330,6 +330,10 @@ def self.matches?(request)
330 330
 
331 331
       resources :content
332 332
 
  333
+      scope :constraints => { :id => /\d+/ } do
  334
+        get '/tickets', :to => 'tickets#index', :as => :tickets
  335
+      end
  336
+
333 337
       match '/:locale/*file.:format', :to => 'files#show', :file => /path\/to\/existing\/file/
334 338
     end
335 339
   end
@@ -1546,6 +1550,14 @@ def test_url_recognition_for_optional_static_segments
1546 1550
     end
1547 1551
   end
1548 1552
 
  1553
+  def test_router_removes_invalid_conditions
  1554
+    with_test_routes do
  1555
+      get '/tickets'
  1556
+      assert_equal 'tickets#index', @response.body
  1557
+      assert_equal '/tickets', tickets_path
  1558
+    end
  1559
+  end
  1560
+
1549 1561
   private
1550 1562
     def with_test_routes
1551 1563
       yield

0 notes on commit 19ccd46

Please sign in to comment.
Something went wrong with that request. Please try again.