FEATURE: Allow themes to specify modifiers in their about.json file (#9097)

There are three modifiers:
- serialize_topic_excerpts (boolean)
- csp_extensions (array of strings)
- svg_icons (array of strings)

When multiple themes are active, the values will be combined. The combination method varies based on the setting. CSP/SVG arrays will be combined. serialize_topic_excerpts will use `Enumerable#any`.
This commit is contained in:
David Taylor
2020-03-11 13:30:45 +00:00
committed by GitHub
parent 0754c7c404
commit d1474e94a1
15 changed files with 205 additions and 6 deletions

View File

@ -17,6 +17,7 @@ class Theme < ActiveRecord::Base
has_many :parent_themes, -> { order(:name) }, through: :parent_theme_relation, source: :parent_theme
has_many :color_schemes
belongs_to :remote_theme, dependent: :destroy
has_one :theme_modifier_set, dependent: :destroy
has_one :settings_field, -> { where(target_id: Theme.targets[:settings], name: "yaml") }, class_name: 'ThemeField'
has_one :javascript_cache, dependent: :destroy
@ -34,6 +35,10 @@ class Theme < ActiveRecord::Base
changed_schemes << scheme if scheme
end
def theme_modifier_set
super || build_theme_modifier_set
end
after_save do
changed_colors.each(&:save!)
changed_schemes.each(&:save!)
@ -44,6 +49,8 @@ class Theme < ActiveRecord::Base
changed_fields.each(&:save!)
changed_fields.clear
theme_modifier_set.save!
if saved_change_to_name?
theme_fields.select(&:basic_html_field?).each(&:invalidate_baked!)
end
@ -112,8 +119,8 @@ class Theme < ActiveRecord::Base
end
def self.get_set_cache(key, &blk)
if val = @cache[key]
return val
if @cache.hash.key? key.to_s
return @cache[key]
end
@cache[key] = blk.call
end
@ -247,6 +254,15 @@ class Theme < ActiveRecord::Base
(@cache[cache_key] = val || "").html_safe
end
def self.lookup_modifier(theme_ids, modifier_name)
theme_ids = [theme_ids] unless Array === theme_ids
theme_ids = transform_ids(theme_ids)
get_set_cache("#{theme_ids.join(",")}:modifier:#{modifier_name}:#{ThemeField::COMPILER_VERSION}") do
ThemeModifierSet.resolve_modifier_for_themes(theme_ids, modifier_name)
end
end
def self.remove_from_cache!
clear_cache!
end
@ -521,6 +537,13 @@ class Theme < ActiveRecord::Base
end
end
meta[:modifiers] = {}.tap do |hash|
ThemeModifierSet::MODIFIERS.keys.each do |modifier|
value = self.theme_modifier_set.public_send(modifier)
hash[modifier] = value if !value.nil?
end
end
meta[:learn_more] = "https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966"
end