Skip to content

Commit

Permalink
Delay loading of Active Record (#244)
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

cc @ClearlyClaire @mjankowski

Co-authored-by: Jean Boussier <[email protected]>
  • Loading branch information
casperisfine and byroot authored Oct 7, 2024
1 parent 2e16f8e commit a9cfbd5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
9 changes: 8 additions & 1 deletion lib/rails-settings-cached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@
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
class ProtectedKeyError < RuntimeError
def initialize(key)
super("Can't use #{key} as setting key.")
end
end

autoload :Base, "rails-settings/base"

module Fields
end
end
8 changes: 2 additions & 6 deletions lib/rails-settings/base.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# frozen_string_literal: true

module RailsSettings
class ProtectedKeyError < RuntimeError
def initialize(key)
super("Can't use #{key} as setting key.")
end
end

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 a9cfbd5

Please sign in to comment.