diff --git a/app/models/theme.rb b/app/models/theme.rb index f38668e1fa6..2906262eaac 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -28,7 +28,7 @@ class Theme < ActiveRecord::Base changed_fields.each(&:save!) changed_fields.clear - Theme.expire_site_cache! if user_selectable_changed? + Theme.expire_site_cache! if user_selectable_changed? || name_changed? @dependant_themes = nil @included_themes = nil @@ -46,7 +46,6 @@ class Theme < ActiveRecord::Base end if self.id - ColorScheme .where(theme_id: self.id) .where("id NOT IN (SELECT color_scheme_id FROM themes where color_scheme_id IS NOT NULL)") @@ -56,6 +55,8 @@ class Theme < ActiveRecord::Base .where(theme_id: self.id) .update_all(theme_id: nil) end + + Theme.expire_site_cache! end after_commit ->(theme) do diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb index 43e1a91fda8..5ff73684628 100644 --- a/spec/models/theme_spec.rb +++ b/spec/models/theme_spec.rb @@ -10,6 +10,10 @@ describe Theme do Fabricate(:user) end + let(:guardian) do + Guardian.new(user) + end + let :customization_params do { name: 'my name', user_id: user.id, header: "my awesome header" } end @@ -214,4 +218,32 @@ HTML expect(Theme.user_theme_keys).to eq(Set.new([])) end + it 'correctly caches user_themes template' do + Theme.destroy_all + + json = Site.json_for(guardian) + user_themes = JSON.parse(json)["user_themes"] + expect(user_themes).to eq([]) + + theme = Theme.create!(name: "bob", user_id: -1, user_selectable: true) + theme.save! + + json = Site.json_for(guardian) + user_themes = JSON.parse(json)["user_themes"].map { |t| t["name"] } + expect(user_themes).to eq(["bob"]) + + theme.name = "sam" + theme.save! + + json = Site.json_for(guardian) + user_themes = JSON.parse(json)["user_themes"].map { |t| t["name"] } + expect(user_themes).to eq(["sam"]) + + Theme.destroy_all + + json = Site.json_for(guardian) + user_themes = JSON.parse(json)["user_themes"] + expect(user_themes).to eq([]) + end + end