Skip to content

Commit af3c866

Browse files
authored
Merge pull request rails#52907 from shioyama/shioyama/lazy_routes_recognize_path
Load routes in `LazyRoutesSet#recognize_path`
2 parents 1ec7b19 + 9960fb7 commit af3c866

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

railties/lib/rails/engine/lazy_route_set.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ def draw(&block)
7373
super
7474
end
7575

76+
def recognize_path(path, environment = {})
77+
Rails.application&.reload_routes_unless_loaded
78+
super
79+
end
80+
7681
def routes
7782
Rails.application&.reload_routes_unless_loaded
7883
super

railties/test/engine/lazy_route_set_test.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class LazyRouteSetTest < ActiveSupport::TestCase
1010
setup :build_app
1111
teardown :teardown_app
1212

13+
class UsersController < ActionController::Base
14+
end
15+
1316
test "app lazily loads routes when invoking url helpers" do
1417
require "#{app_path}/config/environment"
1518

@@ -63,11 +66,11 @@ class LazyRouteSetTest < ActiveSupport::TestCase
6366

6467
@app = Rails.application
6568

66-
assert_not_operator(:users_path, :in?, app_url_helpers.methods)
67-
assert_equal "/users", app_url_helpers.url_for(
68-
controller: :users, action: :index, only_path: true,
69+
assert_not_operator(:products_path, :in?, app_url_helpers.methods)
70+
assert_equal "/products", app_url_helpers.url_for(
71+
controller: :products, action: :index, only_path: true,
6972
)
70-
assert_operator(:users_path, :in?, app_url_helpers.methods)
73+
assert_operator(:products_path, :in?, app_url_helpers.methods)
7174
end
7275

7376
test "engine lazily loads routes when url_for is used" do
@@ -97,6 +100,15 @@ class MyRailtie < ::Rails::Railtie
97100
assert_operator(Rails.application.routes, :is_a?, Engine::LazyRouteSet)
98101
end
99102

103+
test "reloads routes when recognize_path is called" do
104+
require "#{app_path}/config/environment"
105+
106+
assert_equal(
107+
{ controller: "rails/engine/lazy_route_set_test/users", action: "index" },
108+
Rails.application.routes.recognize_path("/users")
109+
)
110+
end
111+
100112
private
101113
def build_app
102114
super
@@ -110,7 +122,8 @@ class User < ActiveRecord::Base
110122
Rails.application.routes.draw do
111123
root to: proc { [200, {}, []] }
112124
113-
resources(:users)
125+
resources :products
126+
resources :users, module: "rails/engine/lazy_route_set_test"
114127
115128
mount Plugin::Engine, at: "/plugin"
116129
end

0 commit comments

Comments
 (0)