aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb17
-rw-r--r--actionpack/test/dispatch/routing/direct_url_helpers_test.rb12
2 files changed, 26 insertions, 3 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 6123a1f5f5..4fc76e8591 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -2094,10 +2094,13 @@ module ActionDispatch
# array passed to `polymorphic_url` is a hash then it's treated as options
# to the url helper that gets called.
#
- # NOTE: The `direct` method doesn't observe the current scope in routes.rb
- # and because of this it's recommended to define them outside of any blocks
- # such as `namespace` or `scope`.
+ # NOTE: The `direct` methodn can't be used inside of a scope block such as
+ # `namespace` or `scope` and will raise an error if it detects that it is.
def direct(name_or_hash, options = nil, &block)
+ unless @scope.root?
+ raise RuntimeError, "The direct method can't be used inside a routes scope block"
+ end
+
case name_or_hash
when Hash
@set.add_polymorphic_mapping(name_or_hash, &block)
@@ -2129,6 +2132,14 @@ module ActionDispatch
scope_level == :nested
end
+ def null?
+ @hash.nil? && @parent.nil?
+ end
+
+ def root?
+ @parent.null?
+ end
+
def resources?
scope_level == :resources
end
diff --git a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb
index 56bb7f13b3..24e46758cc 100644
--- a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb
+++ b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb
@@ -212,4 +212,16 @@ class TestDirectUrlHelpers < ActionDispatch::IntegrationTest
end
end
end
+
+ def test_defining_inside_a_scope_raises_runtime_error
+ routes = ActionDispatch::Routing::RouteSet.new
+
+ assert_raises RuntimeError do
+ routes.draw do
+ namespace :admin do
+ direct(:rubyonrails) { "http://www.rubyonrails.org" }
+ end
+ end
+ end
+ end
end