Skip to content

Commit 76c2613

Browse files
Fix inclusion of url_helpers module in concern
Follow-up to rails#46530. The dynamically generated `url_helpers` module is an `ActiveSupport::Concern`. Therefore, when it is included directly in another `ActiveSupport::Concern`, its `included` block is deferred until the latter concern is itself included elsewhere. Thus, in that case, the call to `base._routes` in `def self.included(base)` will raise `NoMethodError` because the `included` block will not yet have defined the `_routes` method. This commit prevents the error by first checking if `base` responds to `_routes`.
1 parent b0048c7 commit 76c2613

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

actionpack/lib/action_dispatch/routing/route_set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ def url_options; {}; end
604604
# `included` block is run only for the initial inclusion of each copy.
605605
def self.included(base)
606606
super
607-
if !base._routes.equal?(@_proxy._routes)
607+
if base.respond_to?(:_routes) && !base._routes.equal?(@_proxy._routes)
608608
@dup_for_reinclude ||= self.dup
609609
base.include @dup_for_reinclude
610610
end

actionpack/test/dispatch/routing/custom_url_helpers_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ def test_resolve_urls
301301
assert_equal "http://www.example.com/manufacturers/apple", Routes.url_helpers.polymorphic_url(@manufacturer)
302302
end
303303

304+
def test_url_helpers_module_can_be_included_directly_in_an_active_support_concern
305+
concern = Module.new do
306+
extend ActiveSupport::Concern
307+
include Routes.url_helpers
308+
end
309+
310+
concerned = Class.new { include concern }.new
311+
312+
assert_equal "http://www.example.com/", concerned.root_url
313+
end
314+
304315
def test_defining_direct_inside_a_scope_raises_runtime_error
305316
routes = ActionDispatch::Routing::RouteSet.new
306317

0 commit comments

Comments
 (0)