2017-01-13 06:46:24 +11:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2017-05-02 10:14:47 +10:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: settings
|
|
|
|
#
|
2018-04-23 19:29:17 +10:00
|
|
|
# id :bigint(8) not null, primary key
|
2017-05-02 10:14:47 +10:00
|
|
|
# var :string not null
|
|
|
|
# value :text
|
|
|
|
# thing_type :string
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2018-04-23 19:29:17 +10:00
|
|
|
# thing_id :bigint(8)
|
2017-05-02 10:14:47 +10:00
|
|
|
#
|
2017-01-13 06:46:24 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
# 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
|
2017-01-13 06:46:24 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
# It is licensed as follows:
|
2017-01-13 06:46:24 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
# Copyright (c) 2006 Alex Wayne
|
|
|
|
# Some additional features added 2009 by Georg Ledermann
|
2017-01-13 13:24:41 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
# 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.
|
2017-01-13 12:42:22 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
class Setting < ApplicationRecord
|
|
|
|
after_commit :rewrite_cache, on: %i(create update)
|
|
|
|
after_commit :expire_cache, on: %i(destroy)
|
2017-01-13 12:42:22 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
# 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) }
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
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]
|
2017-01-13 12:42:22 +11:00
|
|
|
end
|
|
|
|
end
|
2024-01-09 22:50:21 +11:00
|
|
|
# rubocop:enable Style/MissingRespondToMissing
|
|
|
|
|
|
|
|
def cache_prefix_by_startup
|
|
|
|
@cache_prefix_by_startup ||= Digest::MD5.hexdigest(default_settings.to_s)
|
|
|
|
end
|
2017-01-13 06:46:24 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
def cache_key(var_name)
|
|
|
|
"rails_settings_cached/#{cache_prefix_by_startup}/#{var_name}"
|
|
|
|
end
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
def [](key)
|
|
|
|
Rails.cache.fetch(cache_key(key)) do
|
2024-01-10 01:01:53 +11:00
|
|
|
db_val = find_by(var: key)
|
2024-01-09 22:50:21 +11:00
|
|
|
db_val ? db_val.value : default_settings[key]
|
2017-01-13 06:46:24 +11:00
|
|
|
end
|
2024-01-09 22:50:21 +11:00
|
|
|
end
|
2017-01-13 06:46:24 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
# set a setting value by [] notation
|
|
|
|
def []=(var_name, value)
|
2024-01-10 01:01:53 +11:00
|
|
|
record = find_or_initialize_by(var: var_name.to_s)
|
2024-01-09 22:50:21 +11:00
|
|
|
record.value = value
|
|
|
|
record.save!
|
2017-01-13 06:46:24 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_settings
|
2024-01-09 22:50:21 +11:00
|
|
|
return @default_settings if defined?(@default_settings)
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2024-01-09 22:50:21 +11:00
|
|
|
content = Rails.root.join('config', 'settings.yml').read
|
|
|
|
hash = content.empty? ? {} : YAML.safe_load(ERB.new(content).result, aliases: true).to_hash
|
2024-01-10 01:01:53 +11:00
|
|
|
@default_settings = (hash[Rails.env] || {}).freeze
|
2017-01-13 06:46:24 +11:00
|
|
|
end
|
|
|
|
end
|
2024-01-09 22:50:21 +11:00
|
|
|
|
|
|
|
# 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
|
2017-01-13 06:46:24 +11:00
|
|
|
end
|