FIX: Reset related site settings on general category delete (#18548)

* FIX: Reset related site settings on general category delete

If the new seeded General category is deleted we also need to delete the
corresponding site setting for it so that we don't try and reference it.

This fixes a bug in the category dropdown composer.

This change creates the `clear_related_site_settings` after destroy
hook that could also be used by other features in the future, like maybe
when we have a `default_category_id` site_setting.

Looks like if `nil` out a site setting it is set to `0`?

```
[9] pry(main)> SiteSetting.general_category_id = nil
  SiteSetting Load (0.4ms)  SELECT "site_settings".* FROM "site_settings" WHERE "site_settings"."name" = 'general_category_id' LIMIT 1
=> nil
[10] pry(main)> SiteSetting.general_category_id
=> 0
```

That is why the tests check if the value is `< 1` and not `nil`.

* Use -1 instead of nil because it is the default
This commit is contained in:
Blake Erickson
2022-10-12 11:09:45 -06:00
committed by GitHub
parent da9ce77ffd
commit efb116d2bd
2 changed files with 19 additions and 0 deletions

View File

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