Skip to content

Commit ee5c49f

Browse files
committed
Allow railties to use lazy routes
Previously, railtie initializers couldn't access app.routes without missing the route_set_class setting and using a regular route set. Now, the route set class config is set early enough that railties don't interfere with lazy routes.
1 parent 34ab9b9 commit ee5c49f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

railties/lib/rails/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def load_seed
590590
config.eager_load_paths.freeze
591591
end
592592

593-
initializer :make_routes_lazy, before: :set_routes_reloader_hook do |app|
593+
initializer :make_routes_lazy, before: :bootstrap_hook do |app|
594594
config.route_set_class = LazyRouteSet if Rails.env.local?
595595
end
596596

railties/test/engine/lazy_route_set_test.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class LazyRouteSetTest < ActiveSupport::TestCase
88
include ActiveSupport::Testing::Isolation
99

1010
setup :build_app
11-
1211
teardown :teardown_app
1312

1413
test "app lazily loads routes when invoking url helpers" do
@@ -83,6 +82,21 @@ class LazyRouteSetTest < ActiveSupport::TestCase
8382
assert_not_operator(:plugin_posts_path, :in?, engine_url_helpers.methods)
8483
end
8584

85+
test "railties can access lazy routes" do
86+
app_file("config/application.rb", <<~RUBY, "a+")
87+
88+
class MyRailtie < ::Rails::Railtie
89+
initializer :some_railtie_init do |app|
90+
app.routes
91+
end
92+
end
93+
RUBY
94+
95+
require "#{app_path}/config/environment"
96+
97+
assert_operator(Rails.application.routes, :is_a?, Engine::LazyRouteSet)
98+
end
99+
86100
private
87101
def build_app
88102
super
@@ -118,6 +132,7 @@ class Engine < ::Rails::Engine
118132
end
119133
end
120134
RUBY
135+
121136
plugin.write "config/routes.rb", <<~RUBY
122137
Plugin::Engine.routes.draw do
123138
root to: proc { [200, {}, []] }

0 commit comments

Comments
 (0)