diff --git a/actionpack/lib/action_dispatch/journey/formatter.rb b/actionpack/lib/action_dispatch/journey/formatter.rb index 36c041f25474a364d09fc79a960879cdeb5b7190..7d1f5ab7c9d1d5da26fe2579533e77f00ebdb3b3 100644 --- a/actionpack/lib/action_dispatch/journey/formatter.rb +++ b/actionpack/lib/action_dispatch/journey/formatter.rb @@ -125,19 +125,10 @@ def non_recursive(cache, options) routes end - module RegexCaseComparator - DEFAULT_INPUT = /[-_.a-zA-Z0-9]+\/[-_.a-zA-Z0-9]+/ - DEFAULT_REGEX = /\A#{DEFAULT_INPUT}\Z/ - - def self.===(regex) - DEFAULT_INPUT == regex - end - end - # Returns an array populated with missing keys if any are present. def missing_keys(route, parts) missing_keys = nil - tests = route.path.requirements + tests = route.path.requirements_for_missing_keys_check route.required_parts.each { |key| case tests[key] when nil @@ -145,13 +136,8 @@ def missing_keys(route, parts) missing_keys ||= [] missing_keys << key end - when RegexCaseComparator - unless RegexCaseComparator::DEFAULT_REGEX.match?(parts[key]) - missing_keys ||= [] - missing_keys << key - end else - unless /\A#{tests[key]}\Z/.match?(parts[key]) + unless tests[key].match?(parts[key]) missing_keys ||= [] missing_keys << key end diff --git a/actionpack/lib/action_dispatch/journey/path/pattern.rb b/actionpack/lib/action_dispatch/journey/path/pattern.rb index eaecf0cc37c8c6e1e9a02fffb061ba39a473f852..0d64c6028ba448b23311b1a5bd3e25cd7ab34dbc 100644 --- a/actionpack/lib/action_dispatch/journey/path/pattern.rb +++ b/actionpack/lib/action_dispatch/journey/path/pattern.rb @@ -177,6 +177,12 @@ def to_regexp @re ||= regexp_visitor.new(@separators, @requirements).accept spec end + def requirements_for_missing_keys_check + @requirements_for_missing_keys_check ||= requirements.each_with_object({}) do |(key, regex), hash| + hash[key] = /\A#{regex}\Z/ + end + end + private def regexp_visitor @anchored ? AnchoredRegexp : UnanchoredRegexp diff --git a/actionpack/test/journey/path/pattern_test.rb b/actionpack/test/journey/path/pattern_test.rb index 77c19369b03b4c32580dd36e493d93c96604c2e0..2a751bd43729de8328caa4356fcf7744f386b1c4 100644 --- a/actionpack/test/journey/path/pattern_test.rb +++ b/actionpack/test/journey/path/pattern_test.rb @@ -289,6 +289,37 @@ def test_named_captures named_captures = { "action" => "list", "format" => "rss" } assert_equal named_captures, match.named_captures end + + def test_requirements_for_missing_keys_check + name_regex = /test/ + + path = Pattern.build( + "/page/:name", + { name: name_regex }, + SEPARATORS, + true + ) + + transformed_regex = path.requirements_for_missing_keys_check[:name] + assert_not_nil transformed_regex + assert_equal(transformed_regex, /\A#{name_regex}\Z/) + end + + def test_requirements_for_missing_keys_check_memoization + name_regex = /test/ + + path = Pattern.build( + "/page/:name", + { name: name_regex }, + SEPARATORS, + true + ) + + first_call = path.requirements_for_missing_keys_check[:name] + second_call = path.requirements_for_missing_keys_check[:name] + + assert_equal(first_call.object_id, second_call.object_id) + end end end end