2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-14 13:30:23 +08:00
|
|
|
require 'base64'
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
class ThemeFieldSerializer < ApplicationSerializer
|
2017-05-10 05:20:28 +08:00
|
|
|
attributes :name, :target, :value, :error, :type_id, :upload_id, :url, :filename
|
|
|
|
|
|
|
|
def include_url?
|
|
|
|
object.upload
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_upload_id?
|
|
|
|
object.upload
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_filename?
|
|
|
|
object.upload
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
object.upload&.url
|
|
|
|
end
|
|
|
|
|
|
|
|
def filename
|
|
|
|
object.upload&.original_filename
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
def target
|
2018-03-05 08:04:23 +08:00
|
|
|
Theme.lookup_target(object.target_id)&.to_s
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2017-04-20 04:46:28 +08:00
|
|
|
|
|
|
|
def include_error?
|
|
|
|
object.error.present?
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2019-01-23 17:20:13 +08:00
|
|
|
class BasicThemeSerializer < ApplicationSerializer
|
2018-08-24 09:30:00 +08:00
|
|
|
attributes :id, :name, :created_at, :updated_at, :default, :component
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
def include_default?
|
2018-07-12 12:18:21 +08:00
|
|
|
object.id == SiteSetting.default_theme_id
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def default
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RemoteThemeSerializer < ApplicationSerializer
|
2020-03-18 06:27:54 +08:00
|
|
|
attributes :id, :remote_url, :remote_version, :local_version, :commits_behind, :branch,
|
2019-01-25 22:19:01 +08:00
|
|
|
:remote_updated_at, :updated_at, :github_diff_link, :last_error_text, :is_git?,
|
|
|
|
:license_url, :about_url, :authors, :theme_version, :minimum_discourse_version, :maximum_discourse_version
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
# wow, AMS has some pretty nutty logic where it tries to find the path here
|
|
|
|
# from action dispatch, tell it not to
|
|
|
|
def about_url
|
|
|
|
object.about_url
|
|
|
|
end
|
2018-08-06 13:29:15 +08:00
|
|
|
|
|
|
|
def include_github_diff_link?
|
|
|
|
github_diff_link.present?
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
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?, :disabled_at
|
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
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
has_many :theme_fields, serializer: ThemeFieldSerializer, embed: :objects
|
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
|
|
|
|
@errors = []
|
|
|
|
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
|
|
|
|
object.settings.map { |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
|