DEV: Allow plugins to add theme modifiers via db migrations (#9192)
This commit is contained in:
parent
6102c287f7
commit
ec2d49d48a
|
@ -145,7 +145,7 @@ class RemoteTheme < ActiveRecord::Base
|
||||||
raise ImportError, I18n.t("themes.import_error.about_json_values", errors: self.errors.full_messages.join(","))
|
raise ImportError, I18n.t("themes.import_error.about_json_values", errors: self.errors.full_messages.join(","))
|
||||||
end
|
end
|
||||||
|
|
||||||
ThemeModifierSet::MODIFIERS.keys.each do |modifier_name|
|
ThemeModifierSet.modifiers.keys.each do |modifier_name|
|
||||||
theme.theme_modifier_set.public_send(:"#{modifier_name}=", theme_info.dig("modifiers", modifier_name.to_s))
|
theme.theme_modifier_set.public_send(:"#{modifier_name}=", theme_info.dig("modifiers", modifier_name.to_s))
|
||||||
end
|
end
|
||||||
if !theme.theme_modifier_set.valid?
|
if !theme.theme_modifier_set.valid?
|
||||||
|
|
|
@ -538,7 +538,7 @@ class Theme < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
meta[:modifiers] = {}.tap do |hash|
|
meta[:modifiers] = {}.tap do |hash|
|
||||||
ThemeModifierSet::MODIFIERS.keys.each do |modifier|
|
ThemeModifierSet.modifiers.keys.each do |modifier|
|
||||||
value = self.theme_modifier_set.public_send(modifier)
|
value = self.theme_modifier_set.public_send(modifier)
|
||||||
hash[modifier] = value if !value.nil?
|
hash[modifier] = value if !value.nil?
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,16 +4,14 @@ class ThemeModifierSet < ActiveRecord::Base
|
||||||
|
|
||||||
belongs_to :theme
|
belongs_to :theme
|
||||||
|
|
||||||
MODIFIERS ||= {
|
def self.modifiers
|
||||||
serialize_topic_excerpts: { combine_mode: :any, type: :boolean },
|
@modifiers ||= self.load_modifiers
|
||||||
csp_extensions: { combine_mode: :flatten, type: :string_array },
|
end
|
||||||
svg_icons: { combine_mode: :flatten, type: :string_array },
|
|
||||||
}
|
|
||||||
|
|
||||||
validate :type_validator
|
validate :type_validator
|
||||||
|
|
||||||
def type_validator
|
def type_validator
|
||||||
MODIFIERS.each do |k, config|
|
ThemeModifierSet.modifiers.each do |k, config|
|
||||||
value = public_send(k)
|
value = public_send(k)
|
||||||
next if value.nil?
|
next if value.nil?
|
||||||
|
|
||||||
|
@ -39,18 +37,41 @@ class ThemeModifierSet < ActiveRecord::Base
|
||||||
# Given the ids of multiple active themes / theme components, this function
|
# Given the ids of multiple active themes / theme components, this function
|
||||||
# will combine them into a 'resolved' behavior
|
# will combine them into a 'resolved' behavior
|
||||||
def self.resolve_modifier_for_themes(theme_ids, modifier_name)
|
def self.resolve_modifier_for_themes(theme_ids, modifier_name)
|
||||||
return nil if !(config = MODIFIERS[modifier_name])
|
return nil if !(config = self.modifiers[modifier_name])
|
||||||
|
|
||||||
all_values = self.where(theme_id: theme_ids).where.not(modifier_name => nil).pluck(modifier_name)
|
all_values = self.where(theme_id: theme_ids).where.not(modifier_name => nil).pluck(modifier_name)
|
||||||
case config[:combine_mode]
|
case config[:type]
|
||||||
when :any
|
when :boolean
|
||||||
all_values.any?
|
all_values.any?
|
||||||
when :flatten
|
when :string_array
|
||||||
all_values.flatten(1)
|
all_values.flatten(1)
|
||||||
else
|
else
|
||||||
raise ThemeModifierSetError "Invalid theme modifier combine_mode"
|
raise ThemeModifierSetError "Invalid theme modifier combine_mode"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# Build the list of modifiers from the DB schema.
|
||||||
|
# This allows plugins to introduce new modifiers by adding columns to the table
|
||||||
|
def self.load_modifiers
|
||||||
|
hash = {}
|
||||||
|
columns_hash.each do |column_name, info|
|
||||||
|
next if ["id", "theme_id"].include?(column_name)
|
||||||
|
|
||||||
|
type = nil
|
||||||
|
if info.type == :string && info.array?
|
||||||
|
type = :string_array
|
||||||
|
elsif info.type == :boolean && !info.array?
|
||||||
|
type = :boolean
|
||||||
|
else
|
||||||
|
raise ThemeModifierSetError "Invalid theme modifier column type" if ![:boolean, :string].include?(info.type)
|
||||||
|
end
|
||||||
|
|
||||||
|
hash[column_name.to_sym] = { type: type }
|
||||||
|
end
|
||||||
|
hash
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
|
|
@ -4,7 +4,7 @@ class ThemeModifierHelper
|
||||||
@theme_ids = theme_ids || request&.env&.[](:resolved_theme_ids)
|
@theme_ids = theme_ids || request&.env&.[](:resolved_theme_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
ThemeModifierSet::MODIFIERS.keys.each do |modifier|
|
ThemeModifierSet.modifiers.keys.each do |modifier|
|
||||||
define_method(modifier) do
|
define_method(modifier) do
|
||||||
Theme.lookup_modifier(@theme_ids, modifier)
|
Theme.lookup_modifier(@theme_ids, modifier)
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,5 +20,11 @@ describe ThemeModifierSet do
|
||||||
|
|
||||||
expect(ThemeModifierSet.resolve_modifier_for_themes([t1.id, t2.id], :serialize_topic_excerpts)).to eq(false)
|
expect(ThemeModifierSet.resolve_modifier_for_themes([t1.id, t2.id], :serialize_topic_excerpts)).to eq(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "builds modifiers list from database" do
|
||||||
|
expect(ThemeModifierSet.modifiers.keys).to include(:serialize_topic_excerpts, :csp_extensions)
|
||||||
|
expect(ThemeModifierSet.modifiers[:serialize_topic_excerpts][:type]).to eq(:boolean)
|
||||||
|
expect(ThemeModifierSet.modifiers[:csp_extensions][:type]).to eq(:string_array)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue