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 @@ +
| Post | +Comment | +Score | +Commenter | +Voter | ++ |
|---|---|---|---|---|---|
| + <%= 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 %> + | +