1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Raise an error if direct is inside a scope block

This commit is contained in:
Andrew White 2017-02-21 08:30:36 +00:00
parent 960e73a96d
commit 80dcfd014b
2 changed files with 26 additions and 3 deletions

View file

@ -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

View file

@ -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