Skip to content

Commit cddf163

Browse files
committed
Make sure after_routes_loaded hook runs on boot
This hook was only running when routes were reloaded, but not on boot. The goal was to run any time routes are loaded. This commit fixes it and adds a test. Fixes rails#50720.
1 parent 62972a1 commit cddf163

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

railties/lib/rails/application/finisher.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def self.complete(_state)
161161
reloader.eager_load = app.config.eager_load
162162
reloader.execute
163163
reloaders << reloader
164+
164165
app.reloader.to_run do
165166
# We configure #execute rather than #execute_if_updated because if
166167
# autoloaded constants are cleared we need to reload routes also in
@@ -175,6 +176,8 @@ def self.complete(_state)
175176
reloader.execute
176177
ActiveSupport.run_load_hooks(:after_routes_loaded, self)
177178
end
179+
180+
ActiveSupport.run_load_hooks(:after_routes_loaded, self)
178181
end
179182

180183
# Set clearing dependencies after the finisher hook to ensure paths

railties/test/application/loading_test.rb

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,12 @@ class User
324324
require "#{rails_root}/config/environment"
325325

326326
get "/c"
327-
assert_equal "3", last_response.body
327+
assert_equal "7", last_response.body
328328

329329
app_file "db/schema.rb", ""
330330

331331
get "/c"
332-
assert_equal "19", last_response.body
332+
assert_equal "43", last_response.body
333333
end
334334

335335
test "routes are only loaded once on boot" do
@@ -356,6 +356,36 @@ class User
356356
assert_equal "1", last_response.body
357357
end
358358

359+
test "after_routes_loaded runs once on boot" do
360+
add_to_config <<-RUBY
361+
config.enable_reloading = true
362+
RUBY
363+
364+
app_file "config/routes.rb", <<-RUBY
365+
$counter ||= 0
366+
$counter += 1
367+
Rails.application.routes.draw do
368+
get '/c', to: lambda { |env| [200, {"Content-Type" => "text/plain"}, [$counter.to_s]] }
369+
end
370+
RUBY
371+
372+
app_file "config/initializers/after_routes_loaded.rb", <<-RUBY
373+
Rails.configuration.after_routes_loaded do
374+
$counter *= 3
375+
end
376+
RUBY
377+
378+
boot_app "development"
379+
380+
require "rack/test"
381+
extend Rack::Test::Methods
382+
383+
require "#{rails_root}/config/environment"
384+
385+
get "/c"
386+
assert_equal "3", last_response.body
387+
end
388+
359389
test "columns migrations also trigger reloading" do
360390
add_to_config <<-RUBY
361391
config.enable_reloading = true

0 commit comments

Comments
 (0)