fix deletion of notification entries when deleting a user

This commit is contained in:
Georg Gadinger 2023-10-18 22:13:25 +02:00
parent a85ce45b25
commit c4da510fe7
2 changed files with 30 additions and 3 deletions

View File

@ -5,15 +5,16 @@ class Notification < ApplicationRecord
belongs_to :target, polymorphic: true belongs_to :target, polymorphic: true
after_create do after_create do
recipient.touch(:notifications_updated_at) recipient.touch(:notifications_updated_at) # rubocop:disable Rails/SkipsModelValidations
end end
after_update do after_update do
recipient.touch(:notifications_updated_at) recipient.touch(:notifications_updated_at) # rubocop:disable Rails/SkipsModelValidations
end end
after_destroy do after_destroy do
recipient.touch(:notifications_updated_at) # recipient might not exist at this point (account deleted, records are cleaned up async)
recipient&.touch(:notifications_updated_at) # rubocop:disable Rails/SkipsModelValidations
end end
class << self class << self

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
require "rails_helper"
describe Notification, type: :model do
describe "associations" do
it { should belong_to(:recipient) }
it { should belong_to(:target) }
end
describe "before_destroy" do
let(:user) { FactoryBot.create(:user) }
let(:answer) { FactoryBot.create(:answer, user: FactoryBot.create(:user)) }
it "does not fail if the user wants to delete their account" do
Notification::QuestionAnswered.create(recipient: user, target: answer)
# this deletes the User record and enqueues the deletion of all
# associated records in sidekiq
user.destroy!
# so let's drain the queues
expect { Sidekiq::Worker.drain_all }.not_to raise_error
end
end
end