From 8ec18359c1d923a58c4301150749a6176b0ccf36 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Fri, 5 Jan 2024 16:25:27 +0100 Subject: [PATCH] Delay loading of Active Record Referencing `ActiveRecord::Base` as soon as the gem is required cause Active Record being loaded earlier than it should and some Rails configurations not to take effect. Ref: https://github.com/rails/rails/issues/49827 Ref: https://github.com/mastodon/mastodon/pull/28609 --- lib/rails-settings-cached.rb | 4 +++- lib/rails-settings/base.rb | 2 ++ lib/rails-settings/railtie.rb | 4 ---- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/rails-settings-cached.rb b/lib/rails-settings-cached.rb index db76a3a..2eb46e6 100644 --- a/lib/rails-settings-cached.rb +++ b/lib/rails-settings-cached.rb @@ -9,7 +9,7 @@ require_relative "rails-settings/fields/integer" require_relative "rails-settings/fields/string" -require_relative "rails-settings/base" + require_relative "rails-settings/configuration" require_relative "rails-settings/request_cache" require_relative "rails-settings/middleware" @@ -17,6 +17,8 @@ require_relative "rails-settings/version" module RailsSettings + autoload :Base, "rails-settings/base" + module Fields end end diff --git a/lib/rails-settings/base.rb b/lib/rails-settings/base.rb index 9fb81bc..f237231 100644 --- a/lib/rails-settings/base.rb +++ b/lib/rails-settings/base.rb @@ -11,6 +11,8 @@ class Base < ActiveRecord::Base PROTECTED_KEYS = %w[var value] self.table_name = table_name_prefix + "settings" + after_commit :clear_cache, on: %i[create update destroy] + # get the value field, YAML decoded def value # rubocop:disable Security/YAMLLoad diff --git a/lib/rails-settings/railtie.rb b/lib/rails-settings/railtie.rb index 5c500b1..8691cf4 100644 --- a/lib/rails-settings/railtie.rb +++ b/lib/rails-settings/railtie.rb @@ -2,10 +2,6 @@ module RailsSettings class Railtie < Rails::Railtie - initializer "rails_settings.active_record.initialization" do - RailsSettings::Base.after_commit :clear_cache, on: %i[create update destroy] - end - initializer "rails_settings.configure_rails_initialization" do |app| app.middleware.use RailsSettings::Middleware end