FIX: correctly resets user_themes template

This commit adds tests for this behaviour and also adds support for
reseting cache when updating a theme name and destroying a theme.
This commit is contained in:
Joffrey JAFFEUX 2017-09-04 13:27:58 +02:00 committed by GitHub
parent 3b7128102c
commit caedefd675
2 changed files with 35 additions and 2 deletions

View File

@ -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

View File

@ -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