2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-14 00:30:23 -05:00
|
|
|
require "base64"
|
|
|
|
|
2019-01-23 04:20:13 -05:00
|
|
|
class ThemeSerializer < BasicThemeSerializer
|
2020-11-16 07:44:09 -05:00
|
|
|
attributes :color_scheme,
|
|
|
|
:color_scheme_id,
|
|
|
|
:user_selectable,
|
|
|
|
:auto_update,
|
|
|
|
:remote_theme_id,
|
|
|
|
:settings,
|
|
|
|
:errors,
|
|
|
|
:supported?,
|
|
|
|
:description,
|
|
|
|
:enabled?,
|
2023-04-24 10:30:51 -04:00
|
|
|
:disabled_at,
|
|
|
|
:theme_fields
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-05-18 04:09:21 -04:00
|
|
|
has_one :user, serializer: UserNameSerializer, embed: :object
|
2019-07-03 04:18:11 -04:00
|
|
|
has_one :disabled_by, serializer: UserNameSerializer, embed: :object
|
2018-05-18 04:09:21 -04:00
|
|
|
|
2019-01-23 04:20:13 -05:00
|
|
|
has_many :child_themes, serializer: BasicThemeSerializer, embed: :objects
|
|
|
|
has_many :parent_themes, serializer: BasicThemeSerializer, embed: :objects
|
2017-04-12 10:52:52 -04:00
|
|
|
has_one :remote_theme, serializer: RemoteThemeSerializer, embed: :objects
|
2019-01-17 06:46:11 -05:00
|
|
|
has_many :translations, serializer: ThemeTranslationSerializer, embed: :objects
|
2017-04-13 10:51:44 -04:00
|
|
|
|
2018-10-03 17:03:06 -04:00
|
|
|
def initialize(theme, options = {})
|
|
|
|
super
|
2023-04-24 10:30:51 -04:00
|
|
|
@include_theme_field_values = options[:include_theme_field_values] || false
|
2018-10-03 17:03:06 -04:00
|
|
|
@errors = []
|
2021-04-28 18:00:37 -04:00
|
|
|
|
|
|
|
object.theme_fields.each { |o| @errors << o.error if o.error }
|
2018-10-03 17:03:06 -04:00
|
|
|
end
|
|
|
|
|
2023-04-24 10:30:51 -04: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 10:51:44 -04:00
|
|
|
def child_themes
|
2018-12-20 12:13:05 -05:00
|
|
|
object.child_themes
|
2017-04-13 10:51:44 -04:00
|
|
|
end
|
2018-03-04 19:04:23 -05:00
|
|
|
|
2019-01-23 04:20:13 -05:00
|
|
|
def parent_themes
|
|
|
|
object.parent_themes
|
|
|
|
end
|
|
|
|
|
2018-03-04 19:04:23 -05:00
|
|
|
def settings
|
|
|
|
object.settings.map { |setting| ThemeSettingsSerializer.new(setting, root: false) }
|
2018-10-03 17:03:06 -04:00
|
|
|
rescue ThemeSettingsParser::InvalidYaml => e
|
|
|
|
@errors << e.message
|
|
|
|
nil
|
2018-03-04 19:04:23 -05:00
|
|
|
end
|
2018-08-23 21:30:00 -04:00
|
|
|
|
|
|
|
def include_child_themes?
|
|
|
|
!object.component?
|
|
|
|
end
|
2018-10-03 17:03:06 -04:00
|
|
|
|
|
|
|
def errors
|
|
|
|
@errors
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_errors?
|
|
|
|
@errors.present?
|
|
|
|
end
|
2019-01-25 09:19:01 -05:00
|
|
|
|
|
|
|
def description
|
|
|
|
object.internal_translations.find { |t| t.key == "theme_metadata.description" }&.value
|
|
|
|
end
|
2019-07-03 04:18:11 -04:00
|
|
|
|
|
|
|
def include_disabled_at?
|
|
|
|
object.component? && !object.enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_disabled_by?
|
|
|
|
include_disabled_at?
|
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|