diff options
author | Andrew White <andrew.white@unboxed.co> | 2017-02-21 08:30:36 +0000 |
---|---|---|
committer | Andrew White <andrew.white@unboxed.co> | 2017-02-21 15:30:47 +0000 |
commit | 80dcfd014b27e560f5c4b07ee5ffa98894d8ff63 (patch) | |
tree | f59a8d525c484af4430839c0fb43b52c46ae4be2 /actionpack | |
parent | 960e73a96df55aecfaf196e35df80f8f0121f51d (diff) | |
download | rails-80dcfd014b27e560f5c4b07ee5ffa98894d8ff63.tar.gz rails-80dcfd014b27e560f5c4b07ee5ffa98894d8ff63.tar.bz2 rails-80dcfd014b27e560f5c4b07ee5ffa98894d8ff63.zip |
Raise an error if `direct` is inside a scope block
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 17 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing/direct_url_helpers_test.rb | 12 |
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 |