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

@@ -1,9 +1,14 @@
class CommentVotesController < ApplicationController
respond_to :js, :json, :xml
before_action :member_only
skip_before_action :api_check
respond_to :js, :json, :xml, :html
rescue_with CommentVote::Error, ActiveRecord::RecordInvalid, status: 422
def index
@comment_votes = CommentVote.includes(:user, comment: [:creator, :post]).paginated_search(params)
respond_with(@comment_votes)
end
def create
@comment = Comment.find(params[:comment_id])
@comment_vote = @comment.vote!(params[:score])

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"

View File

@@ -0,0 +1,56 @@
<div id="c-comment-votes">
<div id="a-index">
<%= search_form_for(comment_votes_path) do |f| %>
<%= f.input :user_name, label: "Voter", input_html: { value: params[:search][:user_name], "data-autocomplete": "user" } %>
<%= f.simple_fields_for :comment do |fc| %>
<%= fc.input :creator_name, label: "Commenter", input_html: { value: params.dig(:search, :comment, :creator_name), "data-autocomplete": "user" } %>
<%= fc.input :post_tags_match, label: "Tags", input_html: { value: params.dig(:search, :comment, :post_tags_match), "data-autocomplete": "tag-query" } %>
<% end %>
<%= f.input :score, collection: [["+1", "1"], ["-1", "-1"]], include_blank: true, selected: params[:search][:score] %>
<%= f.submit "Search" %>
<% end %>
<table class="striped autofit">
<thead>
<tr>
<th>Post</th>
<th>Comment</th>
<th>Score</th>
<th>Commenter</th>
<th>Voter</th>
<th></th>
</tr>
</thead>
<tbody>
<% @comment_votes.each do |vote| %>
<tr>
<td>
<%= PostPresenter.preview(vote.comment.post, show_deleted: true) %>
</td>
<td class="col-expand">
<div class="prose">
<%= format_text(vote.comment.body) %>
</div>
</td>
<td><%= link_to sprintf("%+d", vote.score), comment_votes_path(search: { score: vote.score }) %></td>
<td>
<%= link_to_user vote.comment.creator %>
<%= link_to "»", comment_votes_path(search: { comment: { creator_name: vote.comment.creator_name }}) %>
<div><%= time_ago_in_words_tagged(vote.comment.created_at) %></div>
</td>
<td>
<%= link_to_user vote.user %>
<%= link_to "»", comment_votes_path(search: { user_name: vote.user.name }) %>
<div><%= time_ago_in_words_tagged(vote.created_at) %></div>
</td>
<td>
<%= link_to "unvote", comment_comment_votes_path(vote.comment), remote: true, method: :delete %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= numbered_paginator(@comment_votes) %>
</div>
</div>

View File

@@ -39,13 +39,13 @@
<li><%= link_to "Edit", edit_comment_path(comment.id), :id => "edit_comment_link_#{comment.id}", :class => "edit_comment_link" %></li>
<% end %>
<li class="comment-vote-up-link">
<%= link_to "Vote up", comment_votes_path(comment_id: comment.id, score: "up"), method: :post, remote: true %>
<%= link_to "Vote up", comment_comment_votes_path(comment_id: comment.id, score: "up"), method: :post, remote: true %>
</li>
<li class="comment-vote-down-link">
<%= link_to "Vote down", comment_votes_path(comment_id: comment.id, score: "down"), method: :post, remote: true %>
<%= link_to "Vote down", comment_comment_votes_path(comment_id: comment.id, score: "down"), method: :post, remote: true %>
</li>
<li class="comment-unvote-link">
<%= link_to "Unvote", comment_votes_path(comment_id: comment.id), method: :delete, remote: true %>
<%= link_to "Unvote", comment_comment_votes_path(comment_id: comment.id), method: :delete, remote: true %>
</li>
<% if CurrentUser.is_moderator? %>
<li>|</li>

View File

@@ -88,6 +88,7 @@
<li><%= link_to("Help", wiki_pages_path(:title => "help:comments")) %></li>
<li><%= link_to("Listing", comments_path) %></li>
<li><%= link_to("Search", search_comments_path) %></li>
<li><%= link_to("Votes", comment_votes_path) %></li>
<li><%= link_to("RSS", comments_path(:atom)) %></li>
</ul>
<ul>

View File

@@ -92,8 +92,9 @@ Rails.application.routes.draw do
post :approve
end
end
resources :comment_votes, only: [:index]
resources :comments do
resource :votes, :controller => "comment_votes", :only => [:create, :destroy]
resource :votes, controller: "comment_votes", only: [:create, :destroy], as: "comment_votes"
collection do
get :search
end