A platform for community discussion. Free, open, simple.
PERF: Remove N+1s from ThemeController#update and #show (#12842)
David Taylor657dff354494
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rbindex eec56598fe5..95f9bbe1e12 100644--- a/app/controllers/admin/themes_controller.rb+++ b/app/controllers/admin/themes_controller.rb@@ -128,16 +128,7 @@ class Admin::ThemesController < Admin::AdminControllerenddef index- @themes = Theme.order(:name).includes(:child_themes,- :parent_themes,- :remote_theme,- :theme_settings,- :settings_field,- :locale_fields,- :user,- :color_scheme,- theme_fields: :upload- )+ @themes = Theme.include_relations.order(:name)@color_schemes = ColorScheme.all.includes(:theme, color_scheme_colors: :color_scheme).to_apayload = {@@ -175,7 +166,7 @@ class Admin::ThemesController < Admin::AdminControllerenddef update- @theme = Theme.find_by(id: params[:id])+ @theme = Theme.include_relations.find_by(id: params[:id])raise Discourse::InvalidParameters.new(:id) unless @themeoriginal_json = ThemeSerializer.new(@theme, root: false).to_json@@ -213,7 +204,7 @@ class Admin::ThemesController < Admin::AdminControllerif @theme.saveupdate_default_theme- @theme.reload+ @theme = Theme.include_relations.find(@theme.id)if (!disables_component && !enables_component) || theme_params.keys.size > 1log_theme_change(original_json, @theme)@@ -249,7 +240,7 @@ class Admin::ThemesController < Admin::AdminControllerenddef show- @theme = Theme.find_by(id: params[:id])+ @theme = Theme.include_relations.find_by(id: params[:id])raise Discourse::InvalidParameters.new(:id) unless @themerender json: ThemeSerializer.new(@theme)diff --git a/app/models/theme.rb b/app/models/theme.rbindex 1ac7d34489c..3c5ce549344 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -38,6 +38,19 @@ class Theme < ActiveRecord::Basewhere('user_selectable OR id = ?', SiteSetting.default_theme_id)}+ scope :include_relations, -> {+ includes(:child_themes,+ :parent_themes,+ :remote_theme,+ :theme_settings,+ :settings_field,+ :locale_fields,+ :user,+ :color_scheme,+ theme_fields: :upload+ )+ }+def notify_color_change(color, scheme: nil)scheme ||= color.color_schemechanged_colors << color if color