comment votes: add index page.

This commit is contained in:
evazion
2019-10-28 14:12:40 -05:00
parent 93b03c04ad
commit d3165f78aa
6 changed files with 86 additions and 5 deletions

View File

@@ -10,6 +10,24 @@ class CommentVote < ApplicationRecord
validate :validate_comment_can_be_down_voted
validates_inclusion_of :score, :in => [-1, 1], :message => "must be 1 or -1"
def self.visible(user = CurrentUser.user)
return all if user.is_admin?
where(user: user)
end
def self.comment_matches(params)
return all if params.blank?
where(comment_id: Comment.search(params).reorder(nil).select(:id))
end
def self.search(params)
q = super
q = q.visible
q = q.search_attributes(params, :comment_id, :user, :score)
q = q.comment_matches(params[:comment])
q.apply_default_order(params)
end
def validate_user_can_vote
if !user.can_comment_vote?
errors.add :base, "You cannot vote on more than 10 comments per hour"