Remove this rule for several reasons: * A single upvote usually isn't enough to matter, especially with the new comment threshold. * It felt weird that trying to vote on a comment could fail. * Disabling the upvote button on your own comments feels weird. * Most other sites allow you to upvote your own comments. * You're allowed to upvote your own uploads, so it doesn't make sense that you can't upvote your own comments.
51 lines
1.1 KiB
Ruby
51 lines
1.1 KiB
Ruby
class CommentVote < ApplicationRecord
|
|
belongs_to :comment
|
|
belongs_to :user
|
|
|
|
validates_presence_of :score
|
|
validates_uniqueness_of :user_id, :scope => :comment_id, :message => "have already voted for this comment"
|
|
validates_inclusion_of :score, :in => [-1, 1], :message => "must be 1 or -1"
|
|
|
|
after_create :update_score_after_create
|
|
after_destroy :update_score_after_destroy
|
|
|
|
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, :comment, :user)
|
|
q.apply_default_order(params)
|
|
end
|
|
|
|
def is_positive?
|
|
score == 1
|
|
end
|
|
|
|
def is_negative?
|
|
score == -1
|
|
end
|
|
|
|
def update_score_after_create
|
|
comment.with_lock do
|
|
comment.update_columns(score: comment.score + score)
|
|
end
|
|
end
|
|
|
|
def update_score_after_destroy
|
|
comment.with_lock do
|
|
comment.update_columns(score: comment.score - score)
|
|
end
|
|
end
|
|
|
|
def self.available_includes
|
|
[:comment, :user]
|
|
end
|
|
end
|