From ed4743c2803666468446d34484cac8154d4ce213 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 28 Nov 2024 11:22:52 -0800 Subject: [PATCH] Fixes for Rails v7.1+ --- lib/no_brainer/autoload.rb | 14 +++++++++++--- lib/no_brainer/railtie.rb | 6 +++++- lib/nobrainer.rb | 4 ++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/no_brainer/autoload.rb b/lib/no_brainer/autoload.rb index 716ec83668..d3baba701e 100644 --- a/lib/no_brainer/autoload.rb +++ b/lib/no_brainer/autoload.rb @@ -14,9 +14,17 @@ def eager_autoload(*constants) end def eager_load! - super - @_autoloads.keys.map { |c| const_get(c) } - .each { |c| c.eager_load! if c.respond_to?(:eager_load!) } + if NoBrainer.rails71? + if @_eagerloaded_constants + @_eagerloaded_constants.map { |const_name| const_get(const_name) } + .each { |c| c.eager_load! if c.respond_to?(:eager_load!) } + @_eagerloaded_constants = nil + end + else + super + @_autoloads.keys.map { |c| const_get(c) } + .each { |c| c.eager_load! if c.respond_to?(:eager_load!) } + end end def autoload_and_include(*constants) diff --git a/lib/no_brainer/railtie.rb b/lib/no_brainer/railtie.rb index d0e814dd78..8b35ef7d40 100644 --- a/lib/no_brainer/railtie.rb +++ b/lib/no_brainer/railtie.rb @@ -19,7 +19,11 @@ class NoBrainer::Railtie < Rails::Railtie # Not the cleanest behavior, but if ActiveRecord does it, why not. unless defined?(ActiveRecord) console = ActiveSupport::Logger.new(STDERR) - Rails.logger.extend ActiveSupport::Logger.broadcast(console) + if NoBrainer.rails71? + Rails.logger.broadcast_to(console) + else + Rails.logger.extend ActiveSupport::Logger.broadcast(console) + end end end diff --git a/lib/nobrainer.rb b/lib/nobrainer.rb index 54d9c3a8bd..386acc30f1 100644 --- a/lib/nobrainer.rb +++ b/lib/nobrainer.rb @@ -49,6 +49,10 @@ def rails6? Gem.loaded_specs['activesupport'].version >= Gem::Version.new('6.0.0') end + def rails71? + Gem.loaded_specs['activesupport'].version >= Gem::Version.new('7.1.0') + end + def eager_load! # XXX This forces all the NoBrainer code to be loaded in memory. # Not to be confused with eager_load() that operates on documents.