2021-04-08 23:40:16 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-10 17:30:57 +08:00
|
|
|
module SettingsModel
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
serialize :settings, JsonbHashSerializer
|
|
|
|
after_initialize :init_default_settings, if: :new_record?
|
|
|
|
end
|
|
|
|
|
2024-04-10 22:55:49 +08:00
|
|
|
def update_simple_setting(key:, value:)
|
|
|
|
raise ArgumentError, 'Unauthorized setting key' unless Extends::WHITELISTED_USER_SETTINGS.include?(key.to_s)
|
|
|
|
|
|
|
|
settings[key] = value
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_nested_setting(key:, id:, value:)
|
|
|
|
raise ArgumentError, 'Unauthorized setting key' unless Extends::WHITELISTED_USER_SETTINGS.include?(key.to_s)
|
|
|
|
|
|
|
|
settings[key] ||= {}
|
|
|
|
settings[key][id.to_s] = value
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
2017-08-10 17:30:57 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def init_default_settings
|
2021-04-08 23:40:16 +08:00
|
|
|
self.settings = self.class::DEFAULT_SETTINGS
|
2017-08-10 17:30:57 +08:00
|
|
|
end
|
|
|
|
end
|