From 3c0598225450d01f5c1e9774188a4fc4377c8fa2 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Fri, 15 Nov 2024 20:16:25 +0100 Subject: [PATCH] Make Devise.mappings work with lazy loaded routes. 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. --- lib/devise.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/devise.rb b/lib/devise.rb index 3d9dc7886..5b7417ed6 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -274,8 +274,14 @@ 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