FIX: properly clean up related color schemes on delete
This commit is contained in:
parent
7a15b44ac3
commit
4ebbad991e
|
@ -44,6 +44,18 @@ class Theme < ActiveRecord::Base
|
|||
if SiteSetting.default_theme_key == self.key
|
||||
Theme.clear_default!
|
||||
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)")
|
||||
.destroy_all
|
||||
|
||||
ColorScheme
|
||||
.where(theme_id: self.id)
|
||||
.update_all(theme_id: nil)
|
||||
end
|
||||
end
|
||||
|
||||
after_commit ->(theme) do
|
||||
|
|
|
@ -5,4 +5,8 @@ class ColorSchemeSerializer < ApplicationSerializer
|
|||
def theme_name
|
||||
object.theme&.name
|
||||
end
|
||||
|
||||
def theme_id
|
||||
object.theme&.id
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,6 +23,21 @@ describe Theme do
|
|||
expect(s.key).not_to eq(nil)
|
||||
end
|
||||
|
||||
it 'can properly clean up color schemes' do
|
||||
theme = Theme.create!(name: 'bob', user_id: -1)
|
||||
scheme = ColorScheme.create!(theme_id: theme.id, name: 'test')
|
||||
scheme2 = ColorScheme.create!(theme_id: theme.id, name: 'test2')
|
||||
|
||||
Theme.create!(name: 'bob', user_id: -1, color_scheme_id: scheme2.id)
|
||||
|
||||
theme.destroy!
|
||||
scheme2.reload
|
||||
|
||||
expect(scheme2).not_to eq(nil)
|
||||
expect(scheme2.theme_id).to eq(nil)
|
||||
expect(ColorScheme.find_by(id: scheme.id)).to eq(nil)
|
||||
end
|
||||
|
||||
it 'can support child themes' do
|
||||
child = Theme.new(name: '2', user_id: user.id)
|
||||
|
||||
|
|
Loading…
Reference in New Issue