2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-14 13:30:23 +08:00
|
|
|
require "base64"
|
|
|
|
|
2019-01-23 17:20:13 +08:00
|
|
|
class ThemeSerializer < BasicThemeSerializer
|
2020-11-16 20:44:09 +08:00
|
|
|
attributes :color_scheme,
|
|
|
|
:color_scheme_id,
|
|
|
|
:user_selectable,
|
|
|
|
:auto_update,
|
|
|
|
:remote_theme_id,
|
|
|
|
:settings,
|
|
|
|
:errors,
|
|
|
|
:supported?,
|
|
|
|
:description,
|
|
|
|
:enabled?,
|
2023-04-24 22:30:51 +08:00
|
|
|
:disabled_at,
|
|
|
|
:theme_fields
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2018-05-18 16:09:21 +08:00
|
|
|
has_one :user, serializer: UserNameSerializer, embed: :object
|
2019-07-03 16:18:11 +08:00
|
|
|
has_one :disabled_by, serializer: UserNameSerializer, embed: :object
|
2018-05-18 16:09:21 +08:00
|
|
|
|
2019-01-23 17:20:13 +08:00
|
|
|
has_many :child_themes, serializer: BasicThemeSerializer, embed: :objects
|
|
|
|
has_many :parent_themes, serializer: BasicThemeSerializer, embed: :objects
|
2017-04-12 22:52:52 +08:00
|
|
|
has_one :remote_theme, serializer: RemoteThemeSerializer, embed: :objects
|
2019-01-17 19:46:11 +08:00
|
|
|
has_many :translations, serializer: ThemeTranslationSerializer, embed: :objects
|
2017-04-13 22:51:44 +08:00
|
|
|
|
2018-10-04 05:03:06 +08:00
|
|
|
def initialize(theme, options = {})
|
|
|
|
super
|
2023-04-24 22:30:51 +08:00
|
|
|
@include_theme_field_values = options[:include_theme_field_values] || false
|
2018-10-04 05:03:06 +08:00
|
|
|
@errors = []
|
2021-04-29 06:00:37 +08:00
|
|
|
|
|
|
|
object.theme_fields.each { |o| @errors << o.error if o.error }
|
2018-10-04 05:03:06 +08:00
|
|
|
end
|
|
|
|
|
2023-04-24 22:30:51 +08:00
|
|
|
def theme_fields
|
|
|
|
ActiveModel::ArraySerializer.new(
|
|
|
|
object.theme_fields,
|
|
|
|
each_serializer: ThemeFieldSerializer,
|
|
|
|
include_value: include_theme_field_values?,
|
|
|
|
).as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_theme_field_values?
|
|
|
|
# This is passed into each `ThemeFieldSerializer` to determine if `value` will be serialized.
|
|
|
|
# We only want to serialize if we are viewing staff_action_logs (for diffing changes), or if
|
|
|
|
# the theme is a local theme, so the saved values appear in the theme field editor.
|
|
|
|
@include_theme_field_values || object.remote_theme_id.nil?
|
|
|
|
end
|
|
|
|
|
2017-04-13 22:51:44 +08:00
|
|
|
def child_themes
|
2018-12-21 01:13:05 +08:00
|
|
|
object.child_themes
|
2017-04-13 22:51:44 +08:00
|
|
|
end
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-01-23 17:20:13 +08:00
|
|
|
def parent_themes
|
|
|
|
object.parent_themes
|
|
|
|
end
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
def settings
|
2024-02-01 10:26:56 +08:00
|
|
|
object.settings.map { |_name, setting| ThemeSettingsSerializer.new(setting, root: false) }
|
2018-10-04 05:03:06 +08:00
|
|
|
rescue ThemeSettingsParser::InvalidYaml => e
|
|
|
|
@errors << e.message
|
|
|
|
nil
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
2018-08-24 09:30:00 +08:00
|
|
|
|
|
|
|
def include_child_themes?
|
|
|
|
!object.component?
|
|
|
|
end
|
2018-10-04 05:03:06 +08:00
|
|
|
|
|
|
|
def errors
|
|
|
|
@errors
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_errors?
|
|
|
|
@errors.present?
|
|
|
|
end
|
2019-01-25 22:19:01 +08:00
|
|
|
|
|
|
|
def description
|
|
|
|
object.internal_translations.find { |t| t.key == "theme_metadata.description" }&.value
|
|
|
|
end
|
2019-07-03 16:18:11 +08:00
|
|
|
|
|
|
|
def include_disabled_at?
|
|
|
|
object.component? && !object.enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_disabled_by?
|
|
|
|
include_disabled_at?
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|