FIX: Correctly clear theme stylesheet cache when changing color scheme

approved
#1

FIX: Correctly clear theme stylesheet cache when changing color scheme

diff --git a/app/models/theme.rb b/app/models/theme.rb
index a96ef39..74b1049 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -52,6 +52,7 @@ class Theme < ActiveRecord::Base
     changed_fields.clear
 
     Theme.expire_site_cache! if saved_change_to_user_selectable? || saved_change_to_name?
+    notify_with_scheme = saved_change_to_color_scheme_id?
 
     reload
     settings_field&.ensure_baked! # Other fields require setting to be **baked**
@@ -60,6 +61,7 @@ class Theme < ActiveRecord::Base
     remove_from_cache!
     clear_cached_settings!
     ColorScheme.hex_cache.clear
+    notify_theme_change(with_scheme: notify_with_scheme)
   end
 
   after_destroy do
@@ -86,10 +88,6 @@ class Theme < ActiveRecord::Base
     SvgSprite.expire_cache
   end
 
-  after_commit ->(theme) do
-    theme.notify_theme_change(with_scheme: theme.saved_change_to_color_scheme_id?)
-  end, on: [:create, :update]
-
   def self.get_set_cache(key, &blk)
     if val = @cache[key]
       return val
diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb
index 9d8fa63..d145540 100644
--- a/spec/models/theme_spec.rb
+++ b/spec/models/theme_spec.rb
@@ -481,6 +481,31 @@ HTML
     expect(ColorScheme.hex_for_name('header_primary')).to eq('333333')
   end
 
+  it "correctly notifies about theme changes" do
+    cs1 = Fabricate(:color_scheme)
+    cs2 = Fabricate(:color_scheme)
+
+    theme = Fabricate(:theme,
+      user_selectable: true,
+      user: user,
+      color_scheme_id: cs1.id
+    )
+
+    messages = MessageBus.track_publish do
+      theme.save!
+    end.filter { |m| m.channel == "/file-change" }
+    expect(messages.count).to eq(1)
+    expect(messages.first.data.map { |d| d[:target] }).to contain_exactly(:desktop_theme, :mobile_theme)
+
+    # With color scheme change:
+    messages = MessageBus.track_publish do
+      theme.color_scheme_id = cs2.id
+      theme.save!
+    end.filter { |m| m.channel == "/file-change" }
+    expect(messages.count).to eq(1)
+    expect(messages.first.data.map { |d| d[:target] }).to contain_exactly(:admin, :desktop, :desktop_theme, :mobile, :mobile_theme)
+  end
+
   it 'handles settings cache correctly' do
     Theme.destroy_all

GitHub sha: e84531a6

Approved #2
#3

This commit has been mentioned on Discourse Meta. There might be relevant details there: