Color scheme optional defer publish (#12972)

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
diff --git a/app/models/color_scheme.rb b/app/models/color_scheme.rb
index 79e38fb..dd2cf02 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!
-
-      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
+      self.class.publish_discourse_stylesheets!(self.id)
+    end
+  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
 

GitHub sha: 11fe13b4

This commit appears in #12972 which was approved by pmusaraj. It was merged by featheredtoast.