From d3165f78aa0620f6254682be72ec9c422e489edb Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 28 Oct 2019 14:12:40 -0500 Subject: [PATCH] comment votes: add index page. --- app/controllers/comment_votes_controller.rb | 7 ++- app/models/comment_vote.rb | 18 ++++++ app/views/comment_votes/index.html.erb | 56 +++++++++++++++++++ .../comments/partials/show/_comment.html.erb | 6 +- app/views/static/site_map.html.erb | 1 + config/routes.rb | 3 +- 6 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 app/views/comment_votes/index.html.erb diff --git a/app/controllers/comment_votes_controller.rb b/app/controllers/comment_votes_controller.rb index 0402c2408..7539dc2a9 100644 --- a/app/controllers/comment_votes_controller.rb +++ b/app/controllers/comment_votes_controller.rb @@ -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]) diff --git a/app/models/comment_vote.rb b/app/models/comment_vote.rb index 86982abfb..11efe5ea0 100644 --- a/app/models/comment_vote.rb +++ b/app/models/comment_vote.rb @@ -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" diff --git a/app/views/comment_votes/index.html.erb b/app/views/comment_votes/index.html.erb new file mode 100644 index 000000000..a0210c550 --- /dev/null +++ b/app/views/comment_votes/index.html.erb @@ -0,0 +1,56 @@ +
+
+ <%= 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 %> + + + + + + + + + + + + + + <% @comment_votes.each do |vote| %> + + + + + + + + + <% end %> + +
PostCommentScoreCommenterVoter
+ <%= PostPresenter.preview(vote.comment.post, show_deleted: true) %> + +
+ <%= format_text(vote.comment.body) %> +
+
<%= link_to sprintf("%+d", vote.score), comment_votes_path(search: { score: vote.score }) %> + <%= link_to_user vote.comment.creator %> + <%= link_to "»", comment_votes_path(search: { comment: { creator_name: vote.comment.creator_name }}) %> +
<%= time_ago_in_words_tagged(vote.comment.created_at) %>
+
+ <%= link_to_user vote.user %> + <%= link_to "»", comment_votes_path(search: { user_name: vote.user.name }) %> +
<%= time_ago_in_words_tagged(vote.created_at) %>
+
+ <%= link_to "unvote", comment_comment_votes_path(vote.comment), remote: true, method: :delete %> +
+ + <%= numbered_paginator(@comment_votes) %> +
+
diff --git a/app/views/comments/partials/show/_comment.html.erb b/app/views/comments/partials/show/_comment.html.erb index b8571e163..73228ae84 100644 --- a/app/views/comments/partials/show/_comment.html.erb +++ b/app/views/comments/partials/show/_comment.html.erb @@ -39,13 +39,13 @@
  • <%= link_to "Edit", edit_comment_path(comment.id), :id => "edit_comment_link_#{comment.id}", :class => "edit_comment_link" %>
  • <% end %> <% if CurrentUser.is_moderator? %>
  • |
  • diff --git a/app/views/static/site_map.html.erb b/app/views/static/site_map.html.erb index 872ddae27..757e34d0e 100644 --- a/app/views/static/site_map.html.erb +++ b/app/views/static/site_map.html.erb @@ -88,6 +88,7 @@
  • <%= link_to("Help", wiki_pages_path(:title => "help:comments")) %>
  • <%= link_to("Listing", comments_path) %>
  • <%= link_to("Search", search_comments_path) %>
  • +
  • <%= link_to("Votes", comment_votes_path) %>
  • <%= link_to("RSS", comments_path(:atom)) %>