Skip to content

Commit

Permalink
Delay loading of Active Record
Browse files Browse the repository at this point in the history
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: rails/rails#49827
Ref: mastodon/mastodon#28609
  • Loading branch information
byroot committed Jan 5, 2024
1 parent d4d86af commit 8ec1835
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/rails-settings-cached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
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"
require_relative "rails-settings/railtie"
require_relative "rails-settings/version"

module RailsSettings
autoload :Base, "rails-settings/base"

module Fields
end
end
2 changes: 2 additions & 0 deletions lib/rails-settings/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions lib/rails-settings/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8ec1835

Please sign in to comment.