1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #37073 from woahdae/fix-hierarchical-route-options

Fix route from "(:a)(:b)" when given only :a or :b
This commit is contained in:
Aaron Patterson 2019-12-12 12:25:19 -08:00 committed by GitHub
commit 285cc00128
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 5 deletions

View file

@ -398,12 +398,21 @@ module ActionDispatch
end end
end end
# Invokes Journey::Router::Utils.normalize_path and ensure that # Invokes Journey::Router::Utils.normalize_path, then ensures that
# (:locale) becomes (/:locale) instead of /(:locale). Except # /(:locale) becomes (/:locale). Except for root cases, where the
# for root cases, where the latter is the correct one. # former is the correct one.
def self.normalize_path(path) def self.normalize_path(path)
path = Journey::Router::Utils.normalize_path(path) path = Journey::Router::Utils.normalize_path(path)
path.gsub!(%r{/(\(+)/?}, '\1/') unless %r{^/(\(+[^)]+\)){1,}$}.match?(path)
# the path for a root URL at this point can be something like
# "/(/:locale)(/:platform)/(:browser)", and we would want
# "/(:locale)(/:platform)(/:browser)"
# reverse "/(", "/((" etc to "(/", "((/" etc
path.gsub!(%r{/(\(+)/?}, '\1/')
# if a path is all optional segments, change the leading "(/" back to
# "/(" so it evaluates to "/" when interpreted with no options.
path.sub!(%r{^(\(+)/}, '/\1') if %r{^(\(+[^)]+\)){1,}$}.match?(path)
path path
end end

View file

@ -1382,7 +1382,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
assert_equal "projects#index", @response.body assert_equal "projects#index", @response.body
end end
def test_optionally_scoped_root_unscoped_access def test_optional_scoped_root_hierarchy
draw do draw do
scope "(:locale)" do scope "(:locale)" do
scope "(:platform)" do scope "(:platform)" do
@ -1394,8 +1394,22 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end end
assert_equal "/", root_path assert_equal "/", root_path
assert_equal "/en", root_path(locale: "en")
assert_equal "/en/osx", root_path(locale: "en", platform: "osx")
assert_equal "/en/osx/chrome",
root_path(locale: "en", platform: "osx", browser: "chrome")
get "/" get "/"
assert_equal "projects#index", @response.body assert_equal "projects#index", @response.body
get "/en"
assert_equal "projects#index", @response.body
get "/en/osx"
assert_equal "projects#index", @response.body
get "/en/osx/chrome"
assert_equal "projects#index", @response.body
end end
def test_scope_with_format_option def test_scope_with_format_option