From 781e4351bef515e83e6e424b28dde8b433c53dc5 Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Thu, 23 Nov 2023 11:19:04 +0100 Subject: [PATCH] Defer loading when active record is properly loaded This is well known issue. We should prefer to code that reference active record only when it's already lazy loaded. It can break other gems initializing process Related: - https://github.com/rails/rails/issues/46567 - https://github.com/paper-trail-gem/paper_trail/commit/fc6c5f69410f1b79e617610d522dbedfa4f04f7f (inspiration) - https://github.com/thoughtbot/factory_bot_rails/pull/432 Fix: https://github.com/DatabaseCleaner/database_cleaner-active_record/issues/89 --- lib/database_cleaner-active_record.rb | 2 +- lib/database_cleaner/active_record/railtie.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 lib/database_cleaner/active_record/railtie.rb diff --git a/lib/database_cleaner-active_record.rb b/lib/database_cleaner-active_record.rb index ad7a57b3..590d907d 100644 --- a/lib/database_cleaner-active_record.rb +++ b/lib/database_cleaner-active_record.rb @@ -1 +1 @@ -require "database_cleaner/active_record" +require "database_cleaner/active_record/railtie" diff --git a/lib/database_cleaner/active_record/railtie.rb b/lib/database_cleaner/active_record/railtie.rb new file mode 100644 index 00000000..b1ce9650 --- /dev/null +++ b/lib/database_cleaner/active_record/railtie.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module DatabaseCleaner::ActiveRecord + class Railtie < ::Rails::Railtie + initializer "database_cleaner-active_record" do + ActiveSupport.on_load(:active_record) do + require "database_cleaner/active_record" + end + end + end +end