Skip to content
This repository
Browse code

Do not show optional (.:format) block for wildcard route [#6605 state…

…:resolved]

This will make the output of `rake routes` to be correctly match to the behavior of the application, as the regular expression used to match the path is greedy and won't capture the format part by default

This commit is the second attempt on fixing the issue, as the regular expression on another commit on `master` was invalid.
  • Loading branch information...
commit 2dc297d0d74c554b9b1da86c0243afcfe95737ab 1 parent 86acbf1
Prem Sichanugrist authored March 29, 2011 tenderlove committed March 29, 2011
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *Rails 3.0.6 (unreleased)*
2 2
 
  3
+* Fixes the output of `rake routes` to be correctly match to the behavior of the application, as the regular expression used to match the path is greedy and won't capture the format part by default [Prem Sichanugrist]
  4
+
3 5
 * Fixes an issue with number_to_human when converting values which are less than 1 but greater than -1 [Josh Kalderimis]
4 6
 
5 7
 * Sensitive query string parameters (specified in config.filter_parameters) will now be filtered out from the request paths in the log file. [Prem Sichanugrist, fxn]
2  actionpack/actionpack.gemspec
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
25 25
   s.add_dependency('i18n',          '~> 0.5.0')
26 26
   s.add_dependency('rack',          '~> 1.2.1')
27 27
   s.add_dependency('rack-test',     '~> 0.5.7')
28  
-  s.add_dependency('rack-mount',    '~> 0.6.13')
  28
+  s.add_dependency('rack-mount',    '~> 0.6.14')
29 29
   s.add_dependency('tzinfo',        '~> 0.3.23')
30 30
   s.add_dependency('erubis',        '~> 2.6.6')
31 31
 end
2  actionpack/lib/action_dispatch/routing/mapper.rb
@@ -106,7 +106,7 @@ def normalize_path(path)
106 106
             if @options[:format] == false
107 107
               @options.delete(:format)
108 108
               path
109  
-            elsif path.include?(":format")
  109
+            elsif path.include?(":format") || path.match(/\*[^\/]+$/)
110 110
               path
111 111
             else
112 112
               "#{path}(.:format)"
56  actionpack/test/dispatch/mapper_test.rb
... ...
@@ -0,0 +1,56 @@
  1
+require 'abstract_unit'
  2
+
  3
+module ActionDispatch
  4
+  module Routing
  5
+    class MapperTest < ActiveSupport::TestCase
  6
+      class FakeSet
  7
+        attr_reader :routes
  8
+
  9
+        def initialize
  10
+          @routes = []
  11
+        end
  12
+
  13
+        def resources_path_names
  14
+          {}
  15
+        end
  16
+
  17
+        def request_class
  18
+          ActionDispatch::Request
  19
+        end
  20
+
  21
+        def add_route(*args)
  22
+          routes << args
  23
+        end
  24
+
  25
+        def conditions
  26
+          routes.map { |x| x[1] }
  27
+        end
  28
+      end
  29
+
  30
+      def test_initialize
  31
+        Mapper.new FakeSet.new
  32
+      end
  33
+
  34
+      def test_map_wildcard
  35
+        fakeset = FakeSet.new
  36
+        mapper = Mapper.new fakeset
  37
+        mapper.match '/*path', :to => 'pages#show', :as => :page
  38
+        assert_equal '/*path', fakeset.conditions.first[:path_info]
  39
+      end
  40
+
  41
+      def test_map_wildcard_with_other_element
  42
+        fakeset = FakeSet.new
  43
+        mapper = Mapper.new fakeset
  44
+        mapper.match '/*path/foo/:bar', :to => 'pages#show', :as => :page
  45
+        assert_equal '/*path/foo/:bar(.:format)', fakeset.conditions.first[:path_info]
  46
+      end
  47
+
  48
+      def test_map_wildcard_with_multiple_wildcard
  49
+        fakeset = FakeSet.new
  50
+        mapper = Mapper.new fakeset
  51
+        mapper.match '/*foo/*bar', :to => 'pages#show', :as => :page
  52
+        assert_equal '/*foo/*bar', fakeset.conditions.first[:path_info]
  53
+      end
  54
+    end
  55
+  end
  56
+end
6  railties/guides/source/routing.textile
Source Rendered
@@ -554,6 +554,12 @@ match '*a/foo/*b' => 'test#index'
554 554
 
555 555
 would match +zoo/woo/foo/bar/baz+ with +params[:a]+ equals +"zoo/woo"+, and +params[:b]+ equals +"bar/baz"+.
556 556
 
  557
+NOTE: If the wildcard segment is the last segment of the route, the format segment will not be added to the path unless you explicitly specify it as a constraint.
  558
+
  559
+<ruby>
  560
+match '/:path(.:format)', :to => 'pages#show', :constraints => { :path => /.+?/ }
  561
+</ruby>
  562
+
557 563
 h4. Redirection
558 564
 
559 565
 You can redirect any path to another path using the +redirect+ helper in your router:

0 notes on commit 2dc297d

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