Skip to content

Commit

Permalink
Handle missing parent when deleting comment
Browse files Browse the repository at this point in the history
When a post is deleted, a comment retraction might race with a post retraction,
causing the comment deletion to raise because of a missing parent
  • Loading branch information
jhass committed Sep 8, 2019
1 parent 995f339 commit 30e54a5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Comment < ApplicationRecord
end

after_destroy do
self.parent.update_comments_counter
participation = author.participations.find_by(target_id: post.id)
parent.update_comments_counter if parent
participation = author.participations.find_by(target_id: commentable_id)
participation.unparticipate! if participation.present?
end

Expand Down
8 changes: 8 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
participations = Participation.where(target_id: comment_alice.commentable_id, author_id: comment_alice.author_id)
expect(participations.first.count).to eq(1)
end

it "should work if parent is gone" do
comment_alice.parent.delete
comment_alice.reload
expect {
comment_alice.destroy
}.to_not raise_error
end
end

describe "#subscribers" do
Expand Down

0 comments on commit 30e54a5

Please sign in to comment.