Permalink
Browse files

Anchors should be allowed on constraints that are not on path segments

  • Loading branch information...
1 parent 1deeaf5 commit 401c1835afb5af1a6f429061ac8484227c34909d @carllerche carllerche committed Nov 17, 2010
Showing with 20 additions and 9 deletions.
  1. +12 −9 actionpack/lib/action_dispatch/routing/mapper.rb
  2. +8 −0 actionpack/test/controller/routing_test.rb
View
21 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -66,6 +66,18 @@ def normalize_options!
end
@options.merge!(default_controller_and_action(to_shorthand))
+
+ requirements.each do |name, requirement|
+ # segment_keys.include?(k.to_s) || k == :controller
+ next unless Regexp === requirement && !constraints[name]
+
+ if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
+ raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
+ end
+ if requirement.multiline?
+ raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
+ end
+ end
end
# match "account/overview"
@@ -113,15 +125,6 @@ def requirements
@requirements ||= (@options[:constraints].is_a?(Hash) ? @options[:constraints] : {}).tap do |requirements|
requirements.reverse_merge!(@scope[:constraints]) if @scope[:constraints]
@options.each { |k, v| requirements[k] = v if v.is_a?(Regexp) }
-
- requirements.values.grep(Regexp).each do |requirement|
- if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
- raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
- end
- if requirement.multiline?
- raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
- end
- end
end
end
View
8 actionpack/test/controller/routing_test.rb
@@ -834,6 +834,14 @@ def test_route_with_parameter_shell
assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
end
+ def test_route_constraints_on_request_object_with_anchors_are_valid
+ assert_nothing_raised do
+ set.draw do
+ match 'page/:id' => 'pages#show', :constraints => { :host => /^foo$/ }
+ end
+ end
+ end
+
def test_route_constraints_with_anchor_chars_are_invalid
assert_raise ArgumentError do
set.draw do

1 comment on commit 401c183

@RISCfuture

Any reason why not? This commit broke my legacy route:

match ':controller/(:action)', constraints: { action: /^auto_complete_for/ }
Please sign in to comment.