2017-01-12 21:46:24 +02:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 07:58:28 +02:00
|
|
|
|
2017-05-02 03:14:47 +03:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: settings
|
|
|
|
#
|
2018-04-23 12:29:17 +03:00
|
|
|
# id :bigint(8) not null, primary key
|
2017-05-02 03:14:47 +03:00
|
|
|
# var :string not null
|
|
|
|
# value :text
|
|
|
|
# thing_type :string
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2018-04-23 12:29:17 +03:00
|
|
|
# thing_id :bigint(8)
|
2017-05-02 03:14:47 +03:00
|
|
|
#
|
2017-01-12 21:46:24 +02:00
|
|
|
|
|
|
|
class Setting < RailsSettings::Base
|
2017-06-08 14:24:28 +03:00
|
|
|
source Rails.root.join('config', 'settings.yml')
|
2017-01-12 21:46:24 +02:00
|
|
|
|
|
|
|
def to_param
|
|
|
|
var
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2017-01-13 03:42:22 +02:00
|
|
|
def [](key)
|
|
|
|
return super(key) unless rails_initialized?
|
2017-01-13 04:24:41 +02:00
|
|
|
|
2023-02-08 08:06:50 +02:00
|
|
|
Rails.cache.fetch(cache_key(key, nil)) do
|
2017-01-13 03:42:22 +02:00
|
|
|
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)
|
2023-02-20 07:58:28 +02:00
|
|
|
|
2017-01-13 03:42:22 +02:00
|
|
|
db_val.value
|
|
|
|
else
|
|
|
|
default_settings[key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-12 21:46:24 +02:00
|
|
|
def all_as_records
|
|
|
|
vars = thing_scoped
|
2021-01-12 10:27:38 +02:00
|
|
|
records = vars.index_by(&:var)
|
2017-01-12 21:46:24 +02:00
|
|
|
|
|
|
|
default_settings.each do |key, default_value|
|
|
|
|
next if records.key?(key) || default_value.is_a?(Hash)
|
2023-02-20 07:58:28 +02:00
|
|
|
|
2017-01-12 21:46:24 +02:00
|
|
|
records[key] = Setting.new(var: key, value: default_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
records
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_settings
|
|
|
|
return {} unless RailsSettings::Default.enabled?
|
2023-02-20 07:58:28 +02:00
|
|
|
|
2017-01-12 21:46:24 +02:00
|
|
|
RailsSettings::Default.instance
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|