scinote-web/app/models/concerns/settings_model.rb

32 lines
748 B
Ruby
Raw Normal View History

2021-04-08 23:40:16 +08:00
# 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
2021-04-08 23:40:16 +08:00
self.settings = self.class::DEFAULT_SETTINGS
end
end