Skip to content

Commit a29bc1c

Browse files
committed
Fix that optimized named routes should also work as singleton methods on the url_helpers module
1 parent 9d03e20 commit a29bc1c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

actionpack/lib/action_dispatch/routing/route_set.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ def define_url_helper(route, name, options)
188188
remove_possible_method :#{selector}
189189
def #{selector}(*args)
190190
if #{optimize_helper?(route)} && args.size == #{route.required_parts.size} && !args.last.is_a?(Hash) && optimize_routes_generation?
191-
options = #{options.inspect}.merge!(url_options)
191+
options = #{options.inspect}
192+
options.merge!(url_options) if respond_to?(:url_options)
192193
options[:path] = "#{optimized_helper(route)}"
193194
ActionDispatch::Http::URL.url_for(options)
194195
else

actionpack/test/dispatch/routing_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,3 +2538,27 @@ def app; Routes end
25382538
assert_equal "bar", @request.params[:bar]
25392539
end
25402540
end
2541+
2542+
class TestOptimizedNamedRoutes < ActionDispatch::IntegrationTest
2543+
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
2544+
app.draw do
2545+
ok = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, []] }
2546+
get '/foo' => ok, as: :foo
2547+
end
2548+
end
2549+
2550+
include Routes.url_helpers
2551+
def app; Routes end
2552+
2553+
test 'enabled when not mounted and default_url_options is empty' do
2554+
assert Routes.url_helpers.optimize_routes_generation?
2555+
end
2556+
2557+
test 'named route called as singleton method' do
2558+
assert_equal '/foo', Routes.url_helpers.foo_path
2559+
end
2560+
2561+
test 'named route called on included module' do
2562+
assert_equal '/foo', foo_path
2563+
end
2564+
end

0 commit comments

Comments
 (0)