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
|
|
|
|
|
|
|
|
class ChildThemeSerializer < 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
|
|
|
|
attributes :id, :remote_url, :remote_version, :local_version, :about_url,
|
2018-08-06 13:29:15 +08:00
|
|
|
:license_url, :commits_behind, :remote_updated_at, :updated_at,
|
2018-09-08 21:24:11 +08:00
|
|
|
:github_diff_link, :last_error_text
|
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
|
|
|
|
|
|
|
|
class ThemeSerializer < ChildThemeSerializer
|
2018-10-04 05:03:06 +08:00
|
|
|
attributes :color_scheme, :color_scheme_id, :user_selectable, :remote_theme_id, :settings, :errors
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2018-05-18 16:09:21 +08:00
|
|
|
has_one :user, serializer: UserNameSerializer, embed: :object
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
has_many :theme_fields, serializer: ThemeFieldSerializer, embed: :objects
|
|
|
|
has_many :child_themes, serializer: ChildThemeSerializer, embed: :objects
|
|
|
|
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
|
|
|
|
|
|
|
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
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2017-11-14 13:30:23 +08:00
|
|
|
|
|
|
|
class ThemeFieldWithEmbeddedUploadsSerializer < ThemeFieldSerializer
|
|
|
|
attributes :raw_upload
|
|
|
|
|
|
|
|
def include_raw_upload?
|
|
|
|
object.upload
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_upload
|
|
|
|
filename = Discourse.store.path_for(object.upload)
|
|
|
|
raw = nil
|
|
|
|
|
|
|
|
if filename
|
|
|
|
raw = File.read(filename)
|
|
|
|
else
|
|
|
|
raw = Discourse.store.download(object.upload).read
|
|
|
|
end
|
|
|
|
|
|
|
|
Base64.encode64(raw)
|
|
|
|
end
|
|
|
|
end
|
2017-12-14 18:40:08 +08:00
|
|
|
|
|
|
|
class ThemeWithEmbeddedUploadsSerializer < ThemeSerializer
|
|
|
|
has_many :theme_fields, serializer: ThemeFieldWithEmbeddedUploadsSerializer, embed: :objects
|
2018-03-05 08:04:23 +08:00
|
|
|
|
|
|
|
def include_settings?
|
|
|
|
false
|
|
|
|
end
|
2017-12-14 18:40:08 +08:00
|
|
|
end
|