aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorMaura Fitzgerald <momochanfitz@gmail.com>2012-11-02 23:19:05 +0000
committerAndrew White <andyw@pixeltrix.co.uk>2012-12-04 14:42:09 +0000
commit4243de6a04559dd5f02618b46f9f183e4e76a552 (patch)
tree2a02b24c47a203e8812f0ee1db38ff1f7ce487d2 /actionpack
parenta9dc44677ca0caa3660a54b191dca5229dc25f4f (diff)
downloadrails-4243de6a04559dd5f02618b46f9f183e4e76a552.tar.gz
rails-4243de6a04559dd5f02618b46f9f183e4e76a552.tar.bz2
rails-4243de6a04559dd5f02618b46f9f183e4e76a552.zip
Fixed issue where routes with globs caused constraints on that glob to
be ignored. A regular expression constraint gets overwritten when the routes.rb file is processed. Changed the overwriting to an ||= instead of an = assignment.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG.md4
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb2
-rw-r--r--actionpack/test/dispatch/routing_test.rb29
3 files changed, 34 insertions, 1 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index a5fd7b5f25..43fd727500 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,4 +1,8 @@
## Rails 4.0.0 (unreleased) ##
+* Fixed a bug that ignores constraints on a glob route. This was caused because the constraint
+ regular expression is overwritten when the `routes.rb` file is processed. Fixes #7924
+
+ *Maura Fitzgerald*
* More descriptive error messages when calling `render :partial` with
an invalid `:layout` argument.
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 0c19b493ab..a3a8a1b509 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -158,7 +158,7 @@ module ActionDispatch
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) }
+ @options.each { |k, v| requirements[k] ||= v if v.is_a?(Regexp) }
end
end
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index 4f5d8fdb7c..cb5299e8d3 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -3065,6 +3065,35 @@ class TestConstraintsAccessingParameters < ActionDispatch::IntegrationTest
end
end
+class TestGlobRoutingMapper < ActionDispatch::IntegrationTest
+ Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
+ app.draw do
+ ok = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, []] }
+
+ get "/*id" => redirect("/not_cars"), :constraints => {id: /dummy/}
+ get "/cars" => ok
+ end
+ end
+
+ #include Routes.url_helpers
+ def app; Routes end
+
+ def test_glob_constraint
+ get "/dummy"
+ assert_equal "301", @response.code
+ assert_equal "/not_cars", @response.header['Location'].match('/[^/]+$')[0]
+ end
+
+ def test_glob_constraint_skip_route
+ get "/cars"
+ assert_equal "200", @response.code
+ end
+ def test_glob_constraint_skip_all
+ get "/missing"
+ assert_equal "404", @response.code
+ end
+end
+
class TestOptimizedNamedRoutes < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do