mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-17 14:46:00 +08:00
31 lines
748 B
Ruby
31 lines
748 B
Ruby
# frozen_string_literal: true
|
|
|
|
module SettingsModel
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
serialize :settings, JsonbHashSerializer
|
|
after_initialize :init_default_settings, if: :new_record?
|
|
end
|
|
|
|
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
|
|
|
|
protected
|
|
|
|
def init_default_settings
|
|
self.settings = self.class::DEFAULT_SETTINGS
|
|
end
|
|
end
|