From 97eed16f4834ce476136681c05cc2d770080c2a3 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 16 Mar 2026 18:44:41 -0300 Subject: [PATCH] Disable Rails lazy routes with Devise This is an attempt at an alternative fix for the Rails 8 integration. Ref.: https://github.com/heartcombo/devise/pull/5728 --- lib/devise.rb | 8 +------- lib/devise/rails.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/devise.rb b/lib/devise.rb index 8e0c85e77..992698116 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -273,14 +273,8 @@ module Test # PRIVATE CONFIGURATION # Store scopes mappings. + mattr_reader :mappings @@mappings = {} - def self.mappings - # Starting from Rails 8.0, routes are lazy-loaded by default in test and development environments. - # However, Devise's mappings are built during the routes loading phase. - # To ensure it works correctly, we need to load the routes first before accessing @@mappings. - Rails.application.try(:reload_routes_unless_loaded) - @@mappings - end # OmniAuth configurations. mattr_reader :omniauth_configs diff --git a/lib/devise/rails.rb b/lib/devise/rails.rb index b5738853f..ce57e0480 100644 --- a/lib/devise/rails.rb +++ b/lib/devise/rails.rb @@ -53,5 +53,14 @@ class Engine < ::Rails::Engine Rails.autoloaders.main.ignore("#{root}/app/mailers/devise/mailer.rb") end end + + # Starting from Rails 8.0, routes are lazy-loaded by default in test and development environments. + # However, Devise's mappings are built during the routes loading phase, so we need to ensure + # routes are eagerly loaded by reverting the route set class back to the default. + initializer "devise.make_routes_eager_load", after: :make_routes_lazy, before: :add_routing_paths do |app| + if app.config.respond_to?(:route_set_class) + app.config.route_set_class = ActionDispatch::Routing::RouteSet + end + end end end