From 11fe13b45e51b4ef9e18653c25cdb341ba4054c8 Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Thu, 6 May 2021 08:26:58 -1000 Subject: [PATCH] Color scheme optional defer publish (#12972) * DEV: add a method of skipping publishing stylesheets afer color scheme save allows a method to publish all stylesheets if we make changes to many stylesheets at once * use after_save_commit for stylesheet change callbacks This may be more reliable for picking up new stylesheet changes via messagebus as after_save does not guarantee the updates exists in the DB yet. * add skip_publish option for create_from_base --- app/models/color_scheme.rb | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/app/models/color_scheme.rb b/app/models/color_scheme.rb index 79e38fb8bd0..dd2cf021cc8 100644 --- a/app/models/color_scheme.rb +++ b/app/models/color_scheme.rb @@ -155,14 +155,15 @@ class ColorScheme < ActiveRecord::Base end attr_accessor :is_base + attr_accessor :skip_publish has_many :color_scheme_colors, -> { order('id ASC') }, dependent: :destroy alias_method :colors, :color_scheme_colors before_save :bump_version - after_save :publish_discourse_stylesheet - after_save :dump_caches + after_save_commit :publish_discourse_stylesheet, unless: :skip_publish + after_save_commit :dump_caches after_destroy :dump_caches belongs_to :theme @@ -241,6 +242,7 @@ class ColorScheme < ActiveRecord::Base end if params[:colors] new_color_scheme.colors = colors + new_color_scheme.skip_publish if params[:skip_publish] new_color_scheme.save new_color_scheme end @@ -303,18 +305,27 @@ class ColorScheme < ActiveRecord::Base def publish_discourse_stylesheet if self.id - Stylesheet::Manager.clear_color_scheme_cache! + self.class.publish_discourse_stylesheets!(self.id) + end + end - theme_ids = Theme.where(color_scheme_id: self.id).pluck(:id) - if theme_ids.present? - Stylesheet::Manager.cache.clear - Theme.notify_theme_change( - theme_ids, - with_scheme: true, - clear_manager_cache: false, - all_themes: true - ) - end + def self.publish_discourse_stylesheets!(id = nil) + Stylesheet::Manager.clear_color_scheme_cache! + + theme_ids = [] + if id + theme_ids = Theme.where(color_scheme_id: id).pluck(:id) + else + theme_ids = Theme.all.pluck(:id) + end + if theme_ids.present? + Stylesheet::Manager.cache.clear + Theme.notify_theme_change( + theme_ids, + with_scheme: true, + clear_manager_cache: false, + all_themes: true + ) end end