Files
danbooru/app/models/comment_vote.rb
evazion 6b91e55283 comments: allow votes to be soft deleted.
Make it so that when a user removes their own vote, the vote is soft
deleted (the is_deleted flag is set) instead of hard deleted.

Changes:

* Add is_deleted flag to comment votes.
* Relax uniqueness constraint so you can have multiple deleted votes on
  the same comment. You can still only have one active vote on the comment.
* Add `soft_delete` method to Deletable concern.
2021-03-30 00:10:22 -05:00

73 lines
1.8 KiB
Ruby

class CommentVote < ApplicationRecord
attr_accessor :updater
belongs_to :comment
belongs_to :user
validate :validate_vote_is_unique, if: :is_deleted_changed?
validates :score, inclusion: { in: [-1, 1], message: "must be 1 or -1" }
before_create :update_score_on_create
before_save :update_score_on_delete_or_undelete, if: -> { !new_record? && is_deleted_changed? }
deletable
def self.visible(user)
if user.is_moderator?
all
elsif user.is_anonymous?
none
else
where(user: user)
end
end
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :score, :is_deleted, :comment, :user)
q.apply_default_order(params)
end
def is_positive?
score == 1
end
def is_negative?
score == -1
end
# allow duplicate deleted votes but not duplicate active votes
def validate_vote_is_unique
if !is_deleted? && CommentVote.active.where.not(id: id).exists?(comment_id: comment_id, user_id: user_id)
errors.add(:user, "have already voted for this comment")
end
end
def update_score_on_create
comment.with_lock do
comment.update_columns(score: comment.score + score)
end
end
def update_score_on_delete_or_undelete
comment.with_lock do
if is_deleted_changed?(from: false, to: true)
comment.update_columns(score: comment.score - score)
if updater != user
ModAction.log("#{updater.name} deleted comment vote ##{id} on comment ##{comment_id}", :comment_vote_delete, updater)
end
else
comment.update_columns(score: comment.score + score)
if updater != user
ModAction.log("#{updater.name} undeleted comment vote ##{id} on comment ##{comment_id}", :comment_vote_undelete, updater)
end
end
end
end
def self.available_includes
[:comment, :user]
end
end