Posts: prune disapprovals on new appeal or flag

This commit is contained in:
nonamethanks
2022-04-11 15:44:54 +02:00
parent 05261bf6d7
commit 63bd5daa3b
3 changed files with 71 additions and 12 deletions

View File

@@ -15,6 +15,7 @@ class PostFlag < ApplicationRecord
validate :validate_post, on: :create
validates :creator_id, uniqueness: { scope: :post_id, on: :create, unless: :is_deletion, message: "have already flagged this post" }
before_save :update_post
after_create :prune_disapprovals
attr_accessor :is_deletion
enum status: {
@@ -90,6 +91,11 @@ class PostFlag < ApplicationRecord
end
end
def prune_disapprovals
return if is_deletion
PostDisapproval.where(post: post).delete_all
end
def update_post
post.update_column(:is_flagged, true) unless post.is_flagged?
end