diff --git a/app/models/category.rb b/app/models/category.rb index adafc49a0f2..0efbb5f3ed8 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -70,6 +70,7 @@ class Category < ActiveRecord::Base after_create :create_category_definition after_destroy :trash_category_definition + after_destroy :clear_related_site_settings before_save :apply_permissions before_save :downcase_email @@ -312,6 +313,12 @@ class Category < ActiveRecord::Base self.topic&.trash! end + def clear_related_site_settings + if self.id == SiteSetting.general_category_id + SiteSetting.general_category_id = -1 + end + end + def topic_url if has_attribute?("topic_slug") Topic.relative_url(topic_id, read_attribute(:topic_slug)) diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 942b38180cd..da0cf3bb4a4 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -1286,4 +1286,16 @@ RSpec.describe Category do end end end + + describe '#deleting the general category' do + fab!(:category) { Fabricate(:category) } + + it 'should empty out the general_category_id site_setting' do + SiteSetting.general_category_id = category.id + category.destroy + + expect(SiteSetting.general_category_id).to_not eq(category.id) + expect(SiteSetting.general_category_id).to be < 1 + end + end end