2019-04-30 04:02:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
require_dependency 'distributed_cache'
|
|
|
|
require_dependency 'stylesheet/compiler'
|
|
|
|
require_dependency 'stylesheet/manager'
|
2018-03-04 19:04:23 -05:00
|
|
|
require_dependency 'theme_settings_parser'
|
|
|
|
require_dependency 'theme_settings_manager'
|
2019-01-17 06:46:11 -05:00
|
|
|
require_dependency 'theme_translation_parser'
|
|
|
|
require_dependency 'theme_translation_manager'
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
class Theme < ActiveRecord::Base
|
2018-07-12 00:18:21 -04:00
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
@cache = DistributedCache.new('theme')
|
|
|
|
|
2018-05-18 04:09:21 -04:00
|
|
|
belongs_to :user
|
2017-04-12 10:52:52 -04:00
|
|
|
belongs_to :color_scheme
|
|
|
|
has_many :theme_fields, dependent: :destroy
|
2018-03-04 19:04:23 -05:00
|
|
|
has_many :theme_settings, dependent: :destroy
|
2019-01-17 06:46:11 -05:00
|
|
|
has_many :theme_translation_overrides, dependent: :destroy
|
2017-04-12 10:52:52 -04:00
|
|
|
has_many :child_theme_relation, class_name: 'ChildTheme', foreign_key: 'parent_theme_id', dependent: :destroy
|
2019-01-23 04:20:13 -05:00
|
|
|
has_many :parent_theme_relation, class_name: 'ChildTheme', foreign_key: 'child_theme_id', dependent: :destroy
|
2018-12-20 12:13:05 -05:00
|
|
|
has_many :child_themes, -> { order(:name) }, through: :child_theme_relation, source: :child_theme
|
2019-01-23 04:20:13 -05:00
|
|
|
has_many :parent_themes, -> { order(:name) }, through: :parent_theme_relation, source: :parent_theme
|
2017-04-17 15:56:13 -04:00
|
|
|
has_many :color_schemes
|
2019-01-25 09:19:01 -05:00
|
|
|
belongs_to :remote_theme, autosave: true
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-12-20 12:13:05 -05:00
|
|
|
has_one :settings_field, -> { where(target_id: Theme.targets[:settings], name: "yaml") }, class_name: 'ThemeField'
|
2019-02-26 09:22:02 -05:00
|
|
|
has_many :locale_fields, -> { filter_locale_fields(I18n.fallbacks[I18n.locale]) }, class_name: 'ThemeField'
|
2018-12-20 12:13:05 -05:00
|
|
|
|
2018-08-23 21:30:00 -04:00
|
|
|
validate :component_validations
|
2018-08-08 00:46:34 -04:00
|
|
|
|
|
|
|
scope :user_selectable, ->() {
|
|
|
|
where('user_selectable OR id = ?', SiteSetting.default_theme_id)
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:56:13 -04:00
|
|
|
def notify_color_change(color)
|
|
|
|
changed_colors << color
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
after_save do
|
2018-03-15 03:26:54 -04:00
|
|
|
color_schemes = {}
|
|
|
|
changed_colors.each do |color|
|
|
|
|
color.save!
|
|
|
|
color_schemes[color.color_scheme_id] ||= color.color_scheme
|
|
|
|
end
|
|
|
|
|
|
|
|
color_schemes.values.each(&:save!)
|
|
|
|
|
2017-04-17 15:56:13 -04:00
|
|
|
changed_colors.clear
|
2018-03-15 03:26:54 -04:00
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
changed_fields.each(&:save!)
|
|
|
|
changed_fields.clear
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
Theme.expire_site_cache! if saved_change_to_user_selectable? || saved_change_to_name?
|
2019-05-08 11:02:55 -04:00
|
|
|
notify_with_scheme = saved_change_to_color_scheme_id?
|
2019-05-24 10:25:55 -04:00
|
|
|
name_changed = saved_change_to_name?
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2019-04-12 06:36:08 -04:00
|
|
|
reload
|
|
|
|
settings_field&.ensure_baked! # Other fields require setting to be **baked**
|
|
|
|
theme_fields.each(&:ensure_baked!)
|
|
|
|
|
2019-05-24 10:25:55 -04:00
|
|
|
if name_changed
|
|
|
|
theme_fields.select { |f| f.basic_html_field? }.each do |f|
|
|
|
|
f.value_baked = nil
|
|
|
|
f.ensure_baked!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
remove_from_cache!
|
2018-03-04 19:04:23 -05:00
|
|
|
clear_cached_settings!
|
2018-09-11 21:04:58 -04:00
|
|
|
ColorScheme.hex_cache.clear
|
2019-05-08 11:02:55 -04:00
|
|
|
notify_theme_change(with_scheme: notify_with_scheme)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after_destroy do
|
|
|
|
remove_from_cache!
|
2018-03-04 19:04:23 -05:00
|
|
|
clear_cached_settings!
|
2018-07-12 00:18:21 -04:00
|
|
|
if SiteSetting.default_theme_id == self.id
|
2017-04-12 10:52:52 -04:00
|
|
|
Theme.clear_default!
|
|
|
|
end
|
2017-05-04 14:03:07 -04:00
|
|
|
|
|
|
|
if self.id
|
|
|
|
ColorScheme
|
|
|
|
.where(theme_id: self.id)
|
|
|
|
.where("id NOT IN (SELECT color_scheme_id FROM themes where color_scheme_id IS NOT NULL)")
|
2017-07-27 21:20:09 -04:00
|
|
|
.destroy_all
|
2017-05-04 14:03:07 -04:00
|
|
|
|
|
|
|
ColorScheme
|
|
|
|
.where(theme_id: self.id)
|
|
|
|
.update_all(theme_id: nil)
|
|
|
|
end
|
2017-09-04 07:27:58 -04:00
|
|
|
|
|
|
|
Theme.expire_site_cache!
|
2018-09-11 21:04:58 -04:00
|
|
|
ColorScheme.hex_cache.clear
|
2019-04-12 06:36:08 -04:00
|
|
|
CSP::Extension.clear_theme_extensions_cache!
|
|
|
|
SvgSprite.expire_cache
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def self.get_set_cache(key, &blk)
|
|
|
|
if val = @cache[key]
|
|
|
|
return val
|
|
|
|
end
|
|
|
|
@cache[key] = blk.call
|
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-07-12 00:18:21 -04:00
|
|
|
def self.theme_ids
|
2018-08-08 00:46:34 -04:00
|
|
|
get_set_cache "theme_ids" do
|
|
|
|
Theme.pluck(:id)
|
2017-04-14 13:35:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-12 00:18:21 -04:00
|
|
|
def self.user_theme_ids
|
2018-08-08 00:46:34 -04:00
|
|
|
get_set_cache "user_theme_ids" do
|
|
|
|
Theme.user_selectable.pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.components_for(theme_id)
|
|
|
|
get_set_cache "theme_components_for_#{theme_id}" do
|
|
|
|
ChildTheme.where(parent_theme_id: theme_id).distinct.pluck(:child_theme_id)
|
2017-04-14 13:35:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def self.expire_site_cache!
|
|
|
|
Site.clear_anon_cache!
|
2018-08-08 00:46:34 -04:00
|
|
|
clear_cache!
|
2017-04-12 10:52:52 -04:00
|
|
|
ApplicationSerializer.expire_cache_fragment!("user_themes")
|
2018-10-15 21:00:33 -04:00
|
|
|
ColorScheme.hex_cache.clear
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_default!
|
2018-07-12 00:18:21 -04:00
|
|
|
SiteSetting.default_theme_id = -1
|
2017-04-12 10:52:52 -04:00
|
|
|
expire_site_cache!
|
|
|
|
end
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def self.transform_ids(ids, extend: true)
|
2019-02-06 10:51:23 -05:00
|
|
|
return [] if ids.nil?
|
2019-01-25 09:19:01 -05:00
|
|
|
get_set_cache "#{extend ? "extended_" : ""}transformed_ids_#{ids.join("_")}" do
|
|
|
|
next [] if ids.blank?
|
|
|
|
|
|
|
|
ids = ids.dup
|
|
|
|
ids.uniq!
|
|
|
|
parent = ids.shift
|
|
|
|
|
|
|
|
components = ids
|
|
|
|
components.push(*components_for(parent)) if extend
|
|
|
|
components.sort!.uniq!
|
2018-08-08 00:46:34 -04:00
|
|
|
|
2019-01-25 09:19:01 -05:00
|
|
|
all_ids = [parent, *components]
|
2018-08-08 00:46:34 -04:00
|
|
|
|
2019-01-25 12:00:19 -05:00
|
|
|
disabled_ids = Theme.where(id: all_ids).includes(:remote_theme)
|
|
|
|
.reject(&:enabled?).pluck(:id)
|
2018-08-08 00:46:34 -04:00
|
|
|
|
2019-01-25 12:00:19 -05:00
|
|
|
all_ids - disabled_ids
|
2019-01-25 09:19:01 -05:00
|
|
|
end
|
2018-08-08 00:46:34 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def set_default!
|
2018-08-23 21:30:00 -04:00
|
|
|
if component
|
2018-08-08 00:46:34 -04:00
|
|
|
raise Discourse::InvalidParameters.new(
|
|
|
|
I18n.t("themes.errors.component_no_default")
|
|
|
|
)
|
|
|
|
end
|
2018-07-12 00:18:21 -04:00
|
|
|
SiteSetting.default_theme_id = id
|
2017-04-12 10:52:52 -04:00
|
|
|
Theme.expire_site_cache!
|
|
|
|
end
|
|
|
|
|
2017-05-03 11:31:16 -04:00
|
|
|
def default?
|
2018-07-12 00:18:21 -04:00
|
|
|
SiteSetting.default_theme_id == id
|
2017-05-03 11:31:16 -04:00
|
|
|
end
|
|
|
|
|
2019-01-25 09:19:01 -05:00
|
|
|
def enabled?
|
|
|
|
if minimum_version = remote_theme&.minimum_discourse_version
|
|
|
|
return false unless Discourse.has_needed_version?(Discourse::VERSION::STRING, minimum_version)
|
|
|
|
end
|
|
|
|
|
|
|
|
if maximum_version = remote_theme&.maximum_discourse_version
|
|
|
|
return false unless Discourse.has_needed_version?(maximum_version, Discourse::VERSION::STRING)
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-08-23 21:30:00 -04:00
|
|
|
def component_validations
|
|
|
|
return unless component
|
|
|
|
|
|
|
|
errors.add(:base, I18n.t("themes.errors.component_no_color_scheme")) if color_scheme_id.present?
|
|
|
|
errors.add(:base, I18n.t("themes.errors.component_no_user_selectable")) if user_selectable
|
|
|
|
errors.add(:base, I18n.t("themes.errors.component_no_default")) if default?
|
|
|
|
end
|
|
|
|
|
|
|
|
def switch_to_component!
|
|
|
|
return if component
|
|
|
|
|
|
|
|
Theme.transaction do
|
|
|
|
self.component = true
|
|
|
|
|
|
|
|
self.color_scheme_id = nil
|
|
|
|
self.user_selectable = false
|
|
|
|
Theme.clear_default! if default?
|
|
|
|
|
|
|
|
ChildTheme.where("parent_theme_id = ?", id).destroy_all
|
|
|
|
self.save!
|
|
|
|
end
|
2018-08-08 00:46:34 -04:00
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-08-23 21:30:00 -04:00
|
|
|
def switch_to_theme!
|
|
|
|
return unless component
|
|
|
|
|
|
|
|
Theme.transaction do
|
|
|
|
self.component = false
|
|
|
|
ChildTheme.where("child_theme_id = ?", id).destroy_all
|
|
|
|
self.save!
|
2018-08-08 00:46:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup_field(theme_ids, target, field)
|
|
|
|
return if theme_ids.blank?
|
|
|
|
theme_ids = [theme_ids] unless Array === theme_ids
|
|
|
|
|
|
|
|
theme_ids = transform_ids(theme_ids)
|
|
|
|
cache_key = "#{theme_ids.join(",")}:#{target}:#{field}:#{ThemeField::COMPILER_VERSION}"
|
2017-04-12 10:52:52 -04:00
|
|
|
lookup = @cache[cache_key]
|
|
|
|
return lookup.html_safe if lookup
|
|
|
|
|
|
|
|
target = target.to_sym
|
2018-08-08 00:46:34 -04:00
|
|
|
val = resolve_baked_field(theme_ids, target, field)
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
(@cache[cache_key] = val || "").html_safe
|
|
|
|
end
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def self.remove_from_cache!
|
2017-04-12 10:52:52 -04:00
|
|
|
clear_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache!
|
|
|
|
@cache.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.targets
|
2019-04-12 06:36:08 -04:00
|
|
|
@targets ||= Enum.new(common: 0, desktop: 1, mobile: 2, settings: 3, translations: 4, extra_scss: 5)
|
2018-03-04 19:04:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup_target(target_id)
|
|
|
|
self.targets.invert[target_id]
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def self.notify_theme_change(theme_ids, with_scheme: false, clear_manager_cache: true, all_themes: false)
|
2017-04-12 10:52:52 -04:00
|
|
|
Stylesheet::Manager.clear_theme_cache!
|
2018-08-08 00:46:34 -04:00
|
|
|
targets = [:mobile_theme, :desktop_theme]
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
if with_scheme
|
|
|
|
targets.prepend(:desktop, :mobile, :admin)
|
|
|
|
Stylesheet::Manager.cache.clear if clear_manager_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
if all_themes
|
|
|
|
message = theme_ids.map { |id| refresh_message_for_targets(targets, id) }.flatten
|
|
|
|
else
|
2019-02-22 16:13:07 -05:00
|
|
|
parent_ids = Theme.where(id: theme_ids).joins(:parent_themes).pluck(:parent_theme_id).uniq
|
|
|
|
message = refresh_message_for_targets(targets, theme_ids | parent_ids).flatten
|
2018-08-08 00:46:34 -04:00
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
MessageBus.publish('/file-change', message)
|
|
|
|
end
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def notify_theme_change(with_scheme: false)
|
2019-01-25 09:19:01 -05:00
|
|
|
theme_ids = Theme.transform_ids([id])
|
2018-08-08 00:46:34 -04:00
|
|
|
self.class.notify_theme_change(theme_ids, with_scheme: with_scheme)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.refresh_message_for_targets(targets, theme_ids)
|
2017-04-12 10:52:52 -04:00
|
|
|
targets.map do |target|
|
2018-08-08 00:46:34 -04:00
|
|
|
Stylesheet::Manager.stylesheet_data(target.to_sym, theme_ids)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def self.resolve_baked_field(theme_ids, target, name)
|
|
|
|
list_baked_fields(theme_ids, target, name).map { |f| f.value_baked || f.value }.join("\n")
|
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def self.list_baked_fields(theme_ids, target, name)
|
|
|
|
target = target.to_sym
|
2019-04-12 06:36:08 -04:00
|
|
|
name = name&.to_sym
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2019-01-17 06:46:11 -05:00
|
|
|
if target == :translations
|
|
|
|
fields = ThemeField.find_first_locale_fields(theme_ids, I18n.fallbacks[name])
|
|
|
|
else
|
|
|
|
fields = ThemeField.find_by_theme_ids(theme_ids)
|
|
|
|
.where(target_id: [Theme.targets[target], Theme.targets[:common]])
|
2019-04-12 06:36:08 -04:00
|
|
|
fields = fields.where(name: name.to_s) unless name.nil?
|
|
|
|
fields = fields.order(:target_id)
|
2019-01-17 06:46:11 -05:00
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
fields.each(&:ensure_baked!)
|
|
|
|
fields
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_baked_field(target, name)
|
2017-07-27 21:20:09 -04:00
|
|
|
list_baked_fields(target, name).map { |f| f.value_baked || f.value }.join("\n")
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_baked_fields(target, name)
|
2019-01-25 09:19:01 -05:00
|
|
|
theme_ids = Theme.transform_ids([id])
|
2018-08-08 00:46:34 -04:00
|
|
|
self.class.list_baked_fields(theme_ids, target, name)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_from_cache!
|
|
|
|
self.class.remove_from_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
def changed_fields
|
|
|
|
@changed_fields ||= []
|
|
|
|
end
|
|
|
|
|
2017-04-17 15:56:13 -04:00
|
|
|
def changed_colors
|
|
|
|
@changed_colors ||= []
|
|
|
|
end
|
|
|
|
|
2017-05-08 11:38:48 -04:00
|
|
|
def set_field(target:, name:, value: nil, type: nil, type_id: nil, upload_id: nil)
|
2017-04-12 10:52:52 -04:00
|
|
|
name = name.to_s
|
|
|
|
|
|
|
|
target_id = Theme.targets[target.to_sym]
|
|
|
|
raise "Unknown target #{target} passed to set field" unless target_id
|
|
|
|
|
2019-01-17 06:46:11 -05:00
|
|
|
type_id ||= type ? ThemeField.types[type.to_sym] : ThemeField.guess_type(name: name, target: target)
|
2017-05-02 16:01:01 -04:00
|
|
|
raise "Unknown type #{type} passed to set field" unless type_id
|
|
|
|
|
2017-05-09 17:20:28 -04:00
|
|
|
value ||= ""
|
2017-05-08 11:38:48 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
field = theme_fields.find { |f| f.name == name && f.target_id == target_id && f.type_id == type_id }
|
2017-04-12 10:52:52 -04:00
|
|
|
if field
|
2017-05-09 17:20:28 -04:00
|
|
|
if value.blank? && !upload_id
|
2017-04-13 17:24:15 -04:00
|
|
|
theme_fields.delete field.destroy
|
2017-04-12 10:52:52 -04:00
|
|
|
else
|
2017-05-09 17:20:28 -04:00
|
|
|
if field.value != value || field.upload_id != upload_id
|
2017-04-12 10:52:52 -04:00
|
|
|
field.value = value
|
2017-05-09 17:20:28 -04:00
|
|
|
field.upload_id = upload_id
|
2017-04-12 10:52:52 -04:00
|
|
|
changed_fields << field
|
|
|
|
end
|
|
|
|
end
|
2019-04-12 06:36:08 -04:00
|
|
|
field
|
2017-04-12 10:52:52 -04:00
|
|
|
else
|
2017-05-08 11:38:48 -04:00
|
|
|
theme_fields.build(target_id: target_id, value: value, name: name, type_id: type_id, upload_id: upload_id) if value.present? || upload_id.present?
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-13 23:22:59 -05:00
|
|
|
def all_theme_variables
|
|
|
|
fields = {}
|
2019-01-25 09:19:01 -05:00
|
|
|
ids = Theme.transform_ids([id])
|
2018-08-08 00:46:34 -04:00
|
|
|
ThemeField.find_by_theme_ids(ids).where(type_id: ThemeField.theme_var_type_ids).each do |field|
|
|
|
|
next if fields.key?(field.name)
|
|
|
|
fields[field.name] = field
|
2017-11-13 23:22:59 -05:00
|
|
|
end
|
|
|
|
fields.values
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def add_child_theme!(theme)
|
2018-08-08 00:46:34 -04:00
|
|
|
new_relation = child_theme_relation.new(child_theme_id: theme.id)
|
|
|
|
if new_relation.save
|
|
|
|
child_themes.reload
|
|
|
|
save!
|
2019-01-25 09:19:01 -05:00
|
|
|
Theme.clear_cache!
|
2018-08-08 00:46:34 -04:00
|
|
|
else
|
|
|
|
raise Discourse::InvalidParameters.new(new_relation.errors.full_messages.join(", "))
|
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
2018-03-04 19:04:23 -05:00
|
|
|
|
2019-01-25 09:19:01 -05:00
|
|
|
def internal_translations
|
|
|
|
translations(internal: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def translations(internal: false)
|
2019-01-17 06:46:11 -05:00
|
|
|
fallbacks = I18n.fallbacks[I18n.locale]
|
|
|
|
begin
|
2019-02-26 09:22:02 -05:00
|
|
|
data = locale_fields.first&.translation_data(with_overrides: false, internal: internal, fallback_fields: locale_fields)
|
2019-01-17 06:46:11 -05:00
|
|
|
return {} if data.nil?
|
|
|
|
best_translations = {}
|
|
|
|
fallbacks.reverse.each do |locale|
|
|
|
|
best_translations.deep_merge! data[locale] if data[locale]
|
|
|
|
end
|
|
|
|
ThemeTranslationManager.list_from_hash(theme: self, hash: best_translations, locale: I18n.locale)
|
|
|
|
rescue ThemeTranslationParser::InvalidYaml
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-04 19:04:23 -05:00
|
|
|
def settings
|
2018-12-20 12:13:05 -05:00
|
|
|
field = settings_field
|
2018-03-04 19:04:23 -05:00
|
|
|
return [] unless field && field.error.nil?
|
|
|
|
|
|
|
|
settings = []
|
|
|
|
ThemeSettingsParser.new(field).load do |name, default, type, opts|
|
|
|
|
settings << ThemeSettingsManager.create(name, default, type, self, opts)
|
|
|
|
end
|
|
|
|
settings
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_settings
|
2018-07-12 00:18:21 -04:00
|
|
|
Rails.cache.fetch("settings_for_theme_#{self.id}", expires_in: 30.minutes) do
|
2018-03-04 19:04:23 -05:00
|
|
|
hash = {}
|
|
|
|
self.settings.each do |setting|
|
|
|
|
hash[setting.name] = setting.value
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cached_settings!
|
2018-07-12 00:18:21 -04:00
|
|
|
Rails.cache.delete("settings_for_theme_#{self.id}")
|
2018-03-04 19:04:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def included_settings
|
|
|
|
hash = {}
|
|
|
|
|
2019-01-25 09:19:01 -05:00
|
|
|
Theme.where(id: Theme.transform_ids([id])).each do |theme|
|
2018-03-04 19:04:23 -05:00
|
|
|
hash.merge!(theme.cached_settings)
|
|
|
|
end
|
|
|
|
|
|
|
|
hash.merge!(self.cached_settings)
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_setting(setting_name, new_value)
|
|
|
|
target_setting = settings.find { |setting| setting.name == setting_name }
|
|
|
|
raise Discourse::NotFound unless target_setting
|
|
|
|
|
|
|
|
target_setting.value = new_value
|
|
|
|
end
|
2019-01-17 06:46:11 -05:00
|
|
|
|
|
|
|
def update_translation(translation_key, new_value)
|
|
|
|
target_translation = translations.find { |translation| translation.key == translation_key }
|
|
|
|
raise Discourse::NotFound unless target_translation
|
|
|
|
target_translation.value = new_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def translation_override_hash
|
|
|
|
hash = {}
|
|
|
|
theme_translation_overrides.each do |override|
|
|
|
|
cursor = hash
|
|
|
|
path = [override.locale] + override.translation_key.split(".")
|
|
|
|
path[0..-2].each do |key|
|
|
|
|
cursor = (cursor[key] ||= {})
|
|
|
|
end
|
|
|
|
cursor[path[-1]] = override.value
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
2019-01-23 09:40:21 -05:00
|
|
|
|
|
|
|
def generate_metadata_hash
|
2019-01-25 09:19:01 -05:00
|
|
|
{}.tap do |meta|
|
|
|
|
meta[:name] = name
|
|
|
|
meta[:component] = component
|
|
|
|
|
|
|
|
RemoteTheme::METADATA_PROPERTIES.each do |property|
|
|
|
|
meta[property] = remote_theme&.public_send(property)
|
2019-01-28 06:55:58 -05:00
|
|
|
meta[property] = nil if meta[property] == "URL" # Clean up old discourse_theme CLI placeholders
|
2019-01-25 09:19:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
meta[:assets] = {}.tap do |hash|
|
2019-01-23 09:40:21 -05:00
|
|
|
theme_fields.where(type_id: ThemeField.types[:theme_upload_var]).each do |field|
|
2019-02-27 04:45:22 -05:00
|
|
|
hash[field.name] = field.file_path
|
2019-01-23 09:40:21 -05:00
|
|
|
end
|
2019-01-25 09:19:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
meta[:color_schemes] = {}.tap do |hash|
|
2019-01-23 09:40:21 -05:00
|
|
|
schemes = self.color_schemes
|
|
|
|
# The selected color scheme may not belong to the theme, so include it anyway
|
|
|
|
schemes = [self.color_scheme] + schemes if self.color_scheme
|
|
|
|
schemes.uniq.each do |scheme|
|
|
|
|
hash[scheme.name] = {}.tap { |colors| scheme.colors.each { |color| colors[color.name] = color.hex } }
|
|
|
|
end
|
|
|
|
end
|
2019-01-25 09:19:01 -05:00
|
|
|
|
2019-01-28 11:00:33 -05:00
|
|
|
meta[:learn_more] = "https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966"
|
2019-01-28 06:34:11 -05:00
|
|
|
|
2019-01-25 09:19:01 -05:00
|
|
|
end
|
2019-01-23 09:40:21 -05:00
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: themes
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2019-01-11 14:29:56 -05:00
|
|
|
# name :string not null
|
2017-04-12 10:52:52 -04:00
|
|
|
# user_id :integer not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# compiler_version :integer default(0), not null
|
|
|
|
# user_selectable :boolean default(FALSE), not null
|
|
|
|
# hidden :boolean default(FALSE), not null
|
|
|
|
# color_scheme_id :integer
|
|
|
|
# remote_theme_id :integer
|
2018-09-19 22:40:51 -04:00
|
|
|
# component :boolean default(FALSE), not null
|
2017-04-12 10:52:52 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_themes_on_remote_theme_id (remote_theme_id) UNIQUE
|
|
|
|
#
|