2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
class ThemeSetting < ActiveRecord::Base
|
|
|
|
belongs_to :theme
|
|
|
|
|
2022-06-09 07:24:30 +08:00
|
|
|
has_many :upload_references, as: :target, dependent: :destroy
|
|
|
|
|
2024-02-08 10:20:59 +08:00
|
|
|
TYPES_ENUM =
|
|
|
|
Enum.new(integer: 0, float: 1, string: 2, bool: 3, list: 4, enum: 5, upload: 6, objects: 7)
|
|
|
|
|
2024-02-21 08:09:37 +08:00
|
|
|
MAXIMUM_JSON_VALUE_SIZE_BYTES = 0.5 * 1024 * 1024 # 0.5 MB
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
validates_presence_of :name, :theme
|
2024-02-08 10:20:59 +08:00
|
|
|
validates :data_type, inclusion: { in: TYPES_ENUM.values }
|
2024-02-21 08:09:37 +08:00
|
|
|
validate :json_value_size, if: -> { self.data_type == TYPES_ENUM[:objects] }
|
2018-03-05 08:04:23 +08:00
|
|
|
validates :name, length: { maximum: 255 }
|
|
|
|
|
2019-04-12 18:36:08 +08:00
|
|
|
after_save :clear_settings_cache
|
|
|
|
after_destroy :clear_settings_cache
|
|
|
|
|
2022-06-09 07:24:30 +08:00
|
|
|
after_save do
|
|
|
|
if self.data_type == ThemeSetting.types[:upload] && saved_change_to_value?
|
|
|
|
UploadReference.ensure_exist!(upload_ids: [self.value], target: self)
|
|
|
|
end
|
2024-10-17 22:16:16 +08:00
|
|
|
|
|
|
|
if theme.theme_modifier_set.refresh_theme_setting_modifiers(
|
|
|
|
target_setting_name: self.name,
|
|
|
|
target_setting_value: self.value,
|
|
|
|
)
|
|
|
|
theme.theme_modifier_set.save!
|
|
|
|
end
|
2022-06-09 07:24:30 +08:00
|
|
|
end
|
|
|
|
|
2019-04-12 18:36:08 +08:00
|
|
|
def clear_settings_cache
|
|
|
|
# All necessary caches will be cleared on next ensure_baked!
|
|
|
|
theme.settings_field&.invalidate_baked!
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.types
|
2024-02-08 10:20:59 +08:00
|
|
|
TYPES_ENUM
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.guess_type(value)
|
|
|
|
case value
|
|
|
|
when Integer
|
|
|
|
types[:integer]
|
|
|
|
when Float
|
|
|
|
types[:float]
|
|
|
|
when String
|
|
|
|
types[:string]
|
|
|
|
when TrueClass, FalseClass
|
|
|
|
types[:bool]
|
|
|
|
end
|
|
|
|
end
|
2024-02-08 10:20:59 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-02-21 08:09:37 +08:00
|
|
|
def json_value_size
|
|
|
|
if json_value.to_json.size > MAXIMUM_JSON_VALUE_SIZE_BYTES
|
|
|
|
errors.add(
|
|
|
|
:json_value,
|
|
|
|
I18n.t(
|
|
|
|
"theme_settings.errors.json_value.too_large",
|
2024-06-12 16:38:40 +08:00
|
|
|
max_size: MAXIMUM_JSON_VALUE_SIZE_BYTES / 1024 / 1024,
|
2024-02-21 08:09:37 +08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: theme_settings
|
|
|
|
#
|
2019-05-03 06:34:12 +08:00
|
|
|
# id :bigint not null, primary key
|
2018-03-05 08:04:23 +08:00
|
|
|
# name :string(255) not null
|
|
|
|
# data_type :integer not null
|
2018-03-05 08:06:45 +08:00
|
|
|
# value :text
|
2018-03-05 08:04:23 +08:00
|
|
|
# theme_id :integer not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2024-02-08 10:20:59 +08:00
|
|
|
# json_value :jsonb
|
2018-03-05 08:04:23 +08:00
|
|
|
#
|