A platform for community discussion. Free, open, simple.
DEV: Refactor updating parent/child relationships between themes (#22244)
Daniel Waterworth6f8056572e97
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rbindex 26733c5e9a5..2eddce7d13c 100644--- a/app/controllers/admin/themes_controller.rb+++ b/app/controllers/admin/themes_controller.rb@@ -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_fieldsupdate_settings@@ -319,24 +317,6 @@ class Admin::ThemesController < Admin::AdminControllerraise 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_themeif theme_params.key?(:default)is_default = theme_params[:default].to_s == "true"diff --git a/app/models/theme.rb b/app/models/theme.rbindex a5b2c24bfbb..a21de2c5398 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -527,6 +527,16 @@ class Theme < ActiveRecord::Baseendend+ 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