diff --git a/app/models/collection.rb b/app/models/collection.rb index 337126b145..329460b574 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -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? diff --git a/app/models/notification.rb b/app/models/notification.rb index 9a1a366869..a8fb5e53bd 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -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 } diff --git a/spec/models/collection_spec.rb b/spec/models/collection_spec.rb index 67b51fdcea..16322aff9f 100644 --- a/spec/models/collection_spec.rb +++ b/spec/models/collection_spec.rb @@ -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