docbrown/config/initializers/rails_settings.rb
Michael Kohl 5406b0576e
Split Settings::Authentication from SiteConfig (#13095)
* Split Settings::Authentication from SiteConfig

* Move specs

* Sort fields

* Update settings usages

* Update recaptcha usages

* Add data update script

* Update spec

* Rename SiteConfigParams concern

* Fixes, new route, new controller

* Controller and service refactoring

* More controller and service updates

* Spec updates

* More spec fixes

* Move file

* Fix FeedbackMessagesController

* Update admin/configs_spec

* Fix remaining specs in admin/configs_spec

* Fix configs API

* Formatting

* Clean up old service object

* Various fixes

* Update DUS

* Add model argument to admin_config_label

* Fix key name

* Fix specs

* Add distinct request caches for settings classes

* Fix e2e tests

* Fix remaining system spec

* Make DUS idempotent

* Move routes block

* Cleanup

* Switch to ActiveSupport::CurrentAttributes

* Pinned rails-settings-cached

* Update e2e test

* Update lib/data_update_scripts/20210316091354_move_authentication_settings.rb

Co-authored-by: rhymes <rhymes@hey.com>

* Add guard to DUS

* Temporarily re-add two SiteConfig fields

* Fix config show view

Co-authored-by: rhymes <rhymes@hey.com>
2021-04-12 09:41:09 +02:00

44 lines
1.4 KiB
Ruby

require "request_store"
# Completely remove original implementation
RailsSettings.__send__(:remove_const, :RequestCache)
module RailsSettings
class Base < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
def self.inherited(subclass)
super
# Define a unique RequestCache class for each settings klass
request_cache = Class.new(ActiveSupport::CurrentAttributes) do
attribute :settings
end
subclass.const_set(:RequestCache, request_cache)
# Override existing methods to use the local RequestCache class
subclass.instance_eval do
define_singleton_method(:cache_key) do
subclass_cache_key = subclass.name.underscore.tr("/", "_")
scope = [subclass_cache_key]
scope << @cache_prefix.call if @cache_prefix
scope.join("/")
end
define_singleton_method(:clear_cache) do
subclass::RequestCache.reset
Rails.cache.delete(cache_key)
end
define_singleton_method(:_all_settings) do
subclass::RequestCache.settings ||=
Rails.cache.fetch(cache_key, expires_in: 1.week) do
vars = unscoped.select("var, value")
result = {}
vars.each { |record| result[record.var] = record.value }
result.with_indifferent_access
end
end
singleton_class.instance_eval { private :_all_settings }
end
end
end
end