aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/routing/mapper.rb
diff options
context:
space:
mode:
authorAndrew White <andrew.white@unboxed.co>2017-02-21 08:30:36 +0000
committerAndrew White <andrew.white@unboxed.co>2017-02-21 15:30:47 +0000
commit80dcfd014b27e560f5c4b07ee5ffa98894d8ff63 (patch)
treef59a8d525c484af4430839c0fb43b52c46ae4be2 /actionpack/lib/action_dispatch/routing/mapper.rb
parent960e73a96df55aecfaf196e35df80f8f0121f51d (diff)
downloadrails-80dcfd014b27e560f5c4b07ee5ffa98894d8ff63.tar.gz
rails-80dcfd014b27e560f5c4b07ee5ffa98894d8ff63.tar.bz2
rails-80dcfd014b27e560f5c4b07ee5ffa98894d8ff63.zip
Raise an error if `direct` is inside a scope block
Diffstat (limited to 'actionpack/lib/action_dispatch/routing/mapper.rb')
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb17
1 files changed, 14 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