Destroy dependent notifications of a collection (#39429)

This commit is contained in:
David Roetzel 2026-06-15 12:15:53 +02:00 committed by Claire
parent fcf222246a
commit dc53b0e077
3 changed files with 17 additions and 1 deletions

View File

@ -33,6 +33,7 @@ class Collection < ApplicationRecord
has_many :accepted_collection_items, -> { accepted }, class_name: 'CollectionItem', inverse_of: :collection # rubocop:disable Rails/HasManyOrHasOneDependent
has_many :collection_reports, dependent: :delete_all
has_many :accounts, -> { merge(CollectionItem.pending_or_accepted) }, through: :collection_items
has_many :notifications, as: :activity, dependent: :destroy
validates :name, presence: true
validates :name, length: { maximum: 40 }, if: :local?

View File

@ -136,7 +136,7 @@ class Notification < ApplicationRecord
belongs_to :generated_annual_report, inverse_of: false
belongs_to :quote, inverse_of: :notification
belongs_to :collection_item, inverse_of: false # TODO: have an inverse?
belongs_to :collection, inverse_of: false # TODO: have an inverse?
belongs_to :collection, inverse_of: :notifications
end
validates :type, inclusion: { in: TYPES }

View File

@ -221,4 +221,19 @@ RSpec.describe Collection do
expect(subject.to_log_permalink).to eq ActivityPub::TagManager.instance.uri_for(subject)
end
end
describe '#destroy' do
let(:collection) { Fabricate(:collection) }
before do
Fabricate(:notification, activity: collection, type: :added_to_collection)
Fabricate(:notification, activity: collection, type: :collection_update)
end
it 'removes the collection and all notifications that reference it' do
expect { collection.destroy }
.to change(described_class, :count).by(-1)
.and change(Notification, :count).by(-2)
end
end
end