Inline what remains of the rails-settings-cached gem (#28618)

Co-authored-by: Claire <claire.github-309c@sitedethib.com>
th-new
Jean Boussier 2024-01-09 12:50:21 +01:00 committed by GitHub
parent 01ca84e541
commit 1781849884
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 157 additions and 214 deletions

View File

@ -75,7 +75,6 @@ gem 'premailer-rails'
gem 'rack-attack', '~> 6.6'
gem 'rack-cors', '~> 2.0', require: 'rack/cors'
gem 'rails-i18n', '~> 7.0'
gem 'rails-settings-cached', '~> 0.6', git: 'https://github.com/mastodon/rails-settings-cached.git', branch: 'v0.6.6-aliases-true'
gem 'redcarpet', '~> 3.6'
gem 'redis', '~> 4.5', require: ['redis', 'redis/connection/hiredis']
gem 'mario-redis-lock', '~> 1.2', require: 'redis_lock'

View File

@ -18,14 +18,6 @@ GIT
sidekiq (>= 3.5)
statsd-ruby (~> 1.4, >= 1.4.0)
GIT
remote: https://github.com/mastodon/rails-settings-cached.git
revision: 86328ef0bd04ce21cc0504ff5e334591e8c2ccab
branch: v0.6.6-aliases-true
specs:
rails-settings-cached (0.6.6)
rails (>= 4.2.0)
GIT
remote: https://github.com/stanhu/omniauth-cas.git
revision: 4211e6d05941b4a981f9a36b49ec166cecd0e271
@ -922,7 +914,6 @@ DEPENDENCIES
rails (~> 7.1.1)
rails-controller-testing (~> 1.0)
rails-i18n (~> 7.0)
rails-settings-cached (~> 0.6)!
rdf-normalize (~> 0.5)
redcarpet (~> 3.6)
redis (~> 4.5)

View File

@ -13,49 +13,120 @@
# thing_id :bigint(8)
#
class Setting < RailsSettings::Base
source Rails.root.join('config', 'settings.yml')
# This file is derived from a fork of the `rails-settings-cached` gem available at
# https://github.com/mastodon/rails-settings-cached/tree/v0.6.6-aliases-true, with
# the original available at:
# https://github.com/huacnlee/rails-settings-cached/tree/0.x
# It is licensed as follows:
# Copyright (c) 2006 Alex Wayne
# Some additional features added 2009 by Georg Ledermann
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
# NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class Setting < ApplicationRecord
after_commit :rewrite_cache, on: %i(create update)
after_commit :expire_cache, on: %i(destroy)
# Settings are server-wide settings only, but they were previously
# used for users too. This can be dropped later with a database
# migration dropping any scoped setting.
default_scope { where(thing_type: nil, thing_id: nil) }
class << self
# get or set a variable with the variable as the called method
# rubocop:disable Style/MissingRespondToMissing
def method_missing(method, *args)
# set a value for a variable
if method.end_with?('=')
var_name = method.to_s.chomp('=')
value = args.first
self[var_name] = value
else
# retrieve a value
self[method.to_s]
end
end
# rubocop:enable Style/MissingRespondToMissing
def object(var_name)
find_by(var: var_name.to_s)
end
def cache_prefix_by_startup
@cache_prefix_by_startup ||= Digest::MD5.hexdigest(default_settings.to_s)
end
def cache_key(var_name)
"rails_settings_cached/#{cache_prefix_by_startup}/#{var_name}"
end
def [](key)
Rails.cache.fetch(cache_key(key)) do
db_val = object(key)
db_val ? db_val.value : default_settings[key]
end
end
# set a setting value by [] notation
def []=(var_name, value)
var_name = var_name.to_s
record = object(var_name) || new(var: var_name)
record.value = value
record.save!
end
def default_settings
return @default_settings if defined?(@default_settings)
content = Rails.root.join('config', 'settings.yml').read
hash = content.empty? ? {} : YAML.safe_load(ERB.new(content).result, aliases: true).to_hash
@default_settings = hash[Rails.env] || {}
end
end
# get the value field, YAML decoded
def value
YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess, Symbol]) if self[:value].present?
end
# set the value field, YAML encoded
def value=(new_value)
self[:value] = new_value.to_yaml
end
def rewrite_cache
Rails.cache.write(cache_key, value)
end
def expire_cache
Rails.cache.delete(cache_key)
end
def cache_key
self.class.cache_key(var)
end
def to_param
var
end
class << self
def [](key)
return super(key) unless rails_initialized?
Rails.cache.fetch(cache_key(key, nil)) do
db_val = object(key)
if db_val
default_value = default_settings[key]
return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash)
db_val.value
else
default_settings[key]
end
end
end
def all_as_records
vars = thing_scoped
records = vars.index_by(&:var)
default_settings.each do |key, default_value|
next if records.key?(key) || default_value.is_a?(Hash)
records[key] = Setting.new(var: key, value: default_value)
end
records
end
def default_settings
return {} unless RailsSettings::Default.enabled?
RailsSettings::Default.instance
end
end
end

View File

@ -13,189 +13,71 @@ RSpec.describe Setting do
end
describe '.[]' do
let(:key) { 'key' }
let(:cache_key) { 'cache-key' }
let(:cache_value) { 'cache-value' }
before do
allow(described_class).to receive(:rails_initialized?).and_return(rails_initialized)
allow(described_class).to receive(:cache_key).with(key).and_return(cache_key)
end
let(:key) { 'key' }
context 'when rails_initialized? is falsey' do
let(:rails_initialized) { false }
it 'calls RailsSettings::Base#[]' do
allow(RailsSettings::Base).to receive(:[]).with(key)
described_class[key]
expect(RailsSettings::Base).to have_received(:[]).with(key)
end
end
context 'when rails_initialized? is truthy' do
context 'when Rails.cache does not exists' do
before do
allow(RailsSettings::Base).to receive(:cache_key).with(key, nil).and_return(cache_key)
allow(described_class).to receive(:object).with(key).and_return(object)
allow(described_class).to receive(:default_settings).and_return(default_settings)
Fabricate(:setting, var: key, value: nil)
Rails.cache.delete(cache_key)
end
let(:rails_initialized) { true }
let(:cache_key) { 'cache-key' }
let(:cache_value) { 'cache-value' }
let(:object) { nil }
let(:default_value) { 'default_value' }
let(:default_settings) { { key => default_value } }
it 'calls not RailsSettings::Base#[]' do
allow(RailsSettings::Base).to receive(:[]).with(key)
it 'calls Setting.object' do
allow(described_class).to receive(:object).with(key)
described_class[key]
expect(RailsSettings::Base).to_not have_received(:[]).with(key)
expect(described_class).to have_received(:object).with(key)
end
context 'when Rails.cache does not exists' do
before do
allow(RailsSettings::Settings).to receive(:object).with(key).and_return(object)
allow(described_class).to receive(:default_settings).and_return(default_settings)
settings_double = instance_double(Settings::ScopedSettings, thing_scoped: records)
allow(Settings::ScopedSettings).to receive(:new).and_return(settings_double)
Rails.cache.delete(cache_key)
context 'when Setting.object returns truthy' do
let(:object) { db_val }
let(:db_val) { instance_double(described_class, value: 'db_val') }
let(:default_value) { 'default_value' }
it 'returns db_val.value' do
expect(described_class[key]).to be db_val.value
end
end
let(:object) { nil }
let(:default_value) { 'default_value' }
let(:default_settings) { { key => default_value } }
let(:records) { [Fabricate(:setting, var: key, value: nil)] }
context 'when Setting.object returns falsey' do
let(:object) { nil }
it 'calls RailsSettings::Settings.object' do
allow(RailsSettings::Settings).to receive(:object).with(key)
it 'returns default_settings[key]' do
expect(described_class[key]).to be default_settings[key]
end
end
end
context 'when Rails.cache exists' do
before do
Rails.cache.write(cache_key, cache_value)
end
it 'does not query the database' do
callback = double
allow(callback).to receive(:call)
ActiveSupport::Notifications.subscribed callback, 'sql.active_record' do
described_class[key]
expect(RailsSettings::Settings).to have_received(:object).with(key)
end
context 'when RailsSettings::Settings.object returns truthy' do
let(:object) { db_val }
let(:db_val) { instance_double(described_class, value: 'db_val') }
context 'when default_value is a Hash' do
let(:default_value) { { default_value: 'default_value' } }
it 'calls default_value.with_indifferent_access.merge!' do
indifferent_hash = instance_double(Hash, merge!: nil)
allow(default_value).to receive(:with_indifferent_access).and_return(indifferent_hash)
described_class[key]
expect(default_value).to have_received(:with_indifferent_access)
expect(indifferent_hash).to have_received(:merge!).with(db_val.value)
end
end
context 'when default_value is not a Hash' do
let(:default_value) { 'default_value' }
it 'returns db_val.value' do
expect(described_class[key]).to be db_val.value
end
end
end
context 'when RailsSettings::Settings.object returns falsey' do
let(:object) { nil }
it 'returns default_settings[key]' do
expect(described_class[key]).to be default_settings[key]
end
end
expect(callback).to_not have_received(:call)
end
context 'when Rails.cache exists' do
before do
Rails.cache.write(cache_key, cache_value)
end
it 'does not query the database' do
callback = double
allow(callback).to receive(:call)
ActiveSupport::Notifications.subscribed callback, 'sql.active_record' do
described_class[key]
end
expect(callback).to_not have_received(:call)
end
it 'returns the cached value' do
expect(described_class[key]).to eq cache_value
end
end
end
end
describe '.all_as_records' do
before do
settings_double = instance_double(Settings::ScopedSettings, thing_scoped: records)
allow(Settings::ScopedSettings).to receive(:new).and_return(settings_double)
allow(described_class).to receive(:default_settings).and_return(default_settings)
end
let(:key) { 'key' }
let(:default_value) { 'default_value' }
let(:default_settings) { { key => default_value } }
let(:original_setting) { Fabricate(:setting, var: key, value: nil) }
let(:records) { [original_setting] }
it 'returns a Hash' do
expect(described_class.all_as_records).to be_a Hash
end
context 'when records includes Setting with var as the key' do
let(:records) { [original_setting] }
it 'includes the original Setting' do
setting = described_class.all_as_records[key]
expect(setting).to eq original_setting
end
end
context 'when records includes nothing' do
let(:records) { [] }
context 'when default_value is not a Hash' do
it 'includes Setting with value of default_value' do
setting = described_class.all_as_records[key]
expect(setting).to be_a described_class
expect(setting).to have_attributes(var: key)
expect(setting).to have_attributes(value: 'default_value')
end
end
context 'when default_value is a Hash' do
let(:default_value) { { 'foo' => 'fuga' } }
it 'returns {}' do
expect(described_class.all_as_records).to eq({})
end
end
end
end
describe '.default_settings' do
subject { described_class.default_settings }
before do
allow(RailsSettings::Default).to receive(:enabled?).and_return(enabled)
end
context 'when RailsSettings::Default.enabled? is false' do
let(:enabled) { false }
it 'returns {}' do
expect(subject).to eq({})
end
end
context 'when RailsSettings::Settings.enabled? is true' do
let(:enabled) { true }
it 'returns instance of RailsSettings::Default' do
expect(subject).to be_a RailsSettings::Default
it 'returns the cached value' do
expect(described_class[key]).to eq cache_value
end
end
end