Skip to content

Commit 55bdfbe

Browse files
committed
Wrap routes.url_helpers.url_for via a proxy
The singleton url_for on Rails.application.routes.url_helpers isn't the same as the url_for you get when you include the module in your class as the latter has support for polymorphic style routes, etc. whereas the former accepts only a hash and is the underlying implementation defined on ActionDispatch::Routing::RouteSet. This commit changes the singleton method to call through a proxy instance so that it gets the full range of features specified in the documentation for url_for.
1 parent 42ad173 commit 55bdfbe

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

actionpack/lib/action_dispatch/routing/route_set.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,17 +446,34 @@ def url_helpers(supports_path = true)
446446

447447
# Define url_for in the singleton level so one can do:
448448
# Rails.application.routes.url_helpers.url_for(args)
449-
@_routes = routes
449+
proxy_class = Class.new do
450+
include UrlFor
451+
include routes.named_routes.path_helpers_module
452+
include routes.named_routes.url_helpers_module
453+
454+
attr_reader :_routes
455+
456+
def initialize(routes)
457+
@_routes = routes
458+
end
459+
460+
def optimize_routes_generation?
461+
@_routes.optimize_routes_generation?
462+
end
463+
end
464+
465+
@_proxy = proxy_class.new(routes)
466+
450467
class << self
451468
def url_for(options)
452-
@_routes.url_for(options)
469+
@_proxy.url_for(options)
453470
end
454471

455472
def optimize_routes_generation?
456-
@_routes.optimize_routes_generation?
473+
@_proxy.optimize_routes_generation?
457474
end
458475

459-
attr_reader :_routes
476+
def _routes; @_proxy._routes; end
460477
def url_options; {}; end
461478
end
462479

0 commit comments

Comments
 (0)