DEV: Refactor updating parent/child relationships between themes (#22244)

The prior add_relative_themes! method was confusing and cleared the
cache repeatedly instead of once for the whole operation.
This commit is contained in:
Daniel Waterworth 2023-06-22 13:57:39 -05:00 committed by GitHub
parent 24c90534fb
commit 6f8056572e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 24 deletions

View File

@ -215,13 +215,11 @@ class Admin::ThemesController < Admin::AdminController
@theme.public_send("#{field}=", theme_params[field]) if theme_params.key?(field)
end
if theme_params.key?(:child_theme_ids)
add_relative_themes!(:child, theme_params[:child_theme_ids])
end
@theme.child_theme_ids = theme_params[:child_theme_ids] if theme_params.key?(:child_theme_ids)
if theme_params.key?(:parent_theme_ids)
add_relative_themes!(:parent, theme_params[:parent_theme_ids])
end
@theme.parent_theme_ids = theme_params[:parent_theme_ids] if theme_params.key?(
:parent_theme_ids,
)
set_fields
update_settings
@ -319,24 +317,6 @@ class Admin::ThemesController < Admin::AdminController
raise Discourse::InvalidAccess if @theme.remote_theme&.is_git?
end
def add_relative_themes!(kind, ids)
expected = ids.map(&:to_i)
relation = kind == :child ? @theme.child_theme_relation : @theme.parent_theme_relation
relation.to_a.each do |relative|
if kind == :child && expected.include?(relative.child_theme_id)
expected.reject! { |id| id == relative.child_theme_id }
elsif kind == :parent && expected.include?(relative.parent_theme_id)
expected.reject! { |id| id == relative.parent_theme_id }
else
relative.destroy
end
end
Theme.where(id: expected).each { |theme| @theme.add_relative_theme!(kind, theme) }
end
def update_default_theme
if theme_params.key?(:default)
is_default = theme_params[:default].to_s == "true"

View File

@ -527,6 +527,16 @@ class Theme < ActiveRecord::Base
end
end
def child_theme_ids=(theme_ids)
super(theme_ids)
DB.after_commit { Theme.clear_cache! }
end
def parent_theme_ids=(theme_ids)
super(theme_ids)
DB.after_commit { Theme.clear_cache! }
end
def add_relative_theme!(kind, theme)
new_relation =
if kind == :child