mirror of
https://github.com/discourse/discourse.git
synced 2025-03-04 19:58:55 +08:00
FIX: Delete associated channel upon category deletion
Currently when a category is deleted, if it has an associated chat channel, the latter won’t be deleted automatically. The fix is quite simple as we were simply missing a `dependent: :destroy` option on the existing relation.
This commit is contained in:
parent
e9863b145c
commit
01392ab90c
@ -0,0 +1,9 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class DeleteOrphanedChannels < ActiveRecord::Migration[7.0]
|
||||||
|
def up
|
||||||
|
DB.exec(
|
||||||
|
"DELETE FROM chat_channels WHERE chatable_type = 'Category' AND type = 'CategoryChannel' AND NOT EXISTS (SELECT * FROM categories WHERE categories.id = chat_channels.chatable_id)",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
@ -5,7 +5,7 @@ module Chat::CategoryExtension
|
|||||||
|
|
||||||
include Chatable
|
include Chatable
|
||||||
|
|
||||||
prepended { has_one :category_channel, as: :chatable }
|
prepended { has_one :category_channel, as: :chatable, dependent: :destroy }
|
||||||
|
|
||||||
def cannot_delete_reason
|
def cannot_delete_reason
|
||||||
return I18n.t("category.cannot_delete.has_chat_channels") if category_channel
|
return I18n.t("category.cannot_delete.has_chat_channels") if category_channel
|
||||||
|
@ -103,9 +103,13 @@ describe Jobs::AutoManageChannelMemberships do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "when chatable doesn’t exist anymore" do
|
context "when chatable doesn’t exist anymore" do
|
||||||
before do
|
let(:channel) do
|
||||||
channel.chatable.destroy!
|
Fabricate(
|
||||||
channel.reload
|
:category_channel,
|
||||||
|
auto_join_users: true,
|
||||||
|
chatable_type: "Category",
|
||||||
|
chatable_id: -1,
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does nothing" do
|
it "does nothing" do
|
||||||
|
@ -8,7 +8,7 @@ RSpec.describe Category do
|
|||||||
let(:channel_class) { CategoryChannel }
|
let(:channel_class) { CategoryChannel }
|
||||||
end
|
end
|
||||||
|
|
||||||
it { is_expected.to have_one(:category_channel) }
|
it { is_expected.to have_one(:category_channel).dependent(:destroy) }
|
||||||
|
|
||||||
describe "#cannot_delete_reason" do
|
describe "#cannot_delete_reason" do
|
||||||
subject(:reason) { category.cannot_delete_reason }
|
subject(:reason) { category.cannot_delete_reason }
|
||||||
|
@ -24,6 +24,10 @@ RSpec.describe CategoriesController do
|
|||||||
it "deletes the category" do
|
it "deletes the category" do
|
||||||
expect { destroy_category }.to change { Category.count }.by(-1)
|
expect { destroy_category }.to change { Category.count }.by(-1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "deletes the associated channel" do
|
||||||
|
expect { destroy_category }.to change { CategoryChannel.count }.by(-1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when channel has messages" do
|
context "when channel has messages" do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user