mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
parent
24c90534fb
commit
6f8056572e
@ -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"
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user