post/comment/forum votes: add vote counts to profile pages.

* Add post/comment/forum vote counts to user profiles.
* Show uploaders on post votes index and allow searching by uploader.
* Show forum posters on forum votes index and allow searching by poster.
* Add unvote link to forum votes index.
* Only show unvote links to current user.
This commit is contained in:
evazion
2019-10-28 14:34:56 -05:00
parent d3165f78aa
commit fd4a9d4d30
7 changed files with 48 additions and 14 deletions

View File

@@ -1,11 +1,11 @@
class PostVotesController < ApplicationController
before_action :voter_only, only: [:create, :destroy]
before_action :voter_only
skip_before_action :api_check
respond_to :js, :json, :xml, :html
rescue_with PostVote::Error, status: 422
def index
@post_votes = PostVote.includes(:post, :user).paginated_search(params)
@post_votes = PostVote.includes(:user, post: [:uploader]).paginated_search(params)
respond_with(@post_votes)
end

View File

@@ -8,9 +8,15 @@ class ForumPostVote < ApplicationRecord
scope :by, ->(user_id) {where(creator_id: user_id)}
scope :excluding_user, ->(user_id) {where("creator_id <> ?", user_id)}
def self.forum_post_matches(params)
return all if params.blank?
where(forum_post_id: ForumPost.search(params).reorder(nil).select(:id))
end
def self.search(params)
q = super
q = q.search_attributes(params, :creator, :forum_post_id, :score)
q = q.forum_post_matches(params[:forum_post])
q.apply_default_order(params)
end

View File

@@ -89,6 +89,7 @@ class User < ApplicationRecord
has_many :artist_versions, foreign_key: :updater_id
has_many :artist_commentary_versions, foreign_key: :updater_id
has_many :comments, foreign_key: :creator_id
has_many :comment_votes, dependent: :destroy
has_many :wiki_page_versions, foreign_key: :updater_id
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
has_many :forum_post_votes, dependent: :destroy, foreign_key: :creator_id

View File

@@ -44,7 +44,9 @@
<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 %>
<% if vote.user == CurrentUser.user %>
<%= link_to "unvote", comment_comment_votes_path(vote.comment), remote: true, method: :delete %>
<% end %>
</td>
</tr>
<% end %>

View File

@@ -1,9 +1,12 @@
<div id="c-forum-post-votes">
<div id="a-index">
<%= search_form_for(forum_post_votes_path) do |f| %>
<%= f.input :creator_name, label: "User", input_html: { value: params[:search][:creator_name], data: { autocomplete: "user" } } %>
<%= f.input :creator_name, label: "Voter", input_html: { value: params[:search][:creator_name], data: { autocomplete: "user" } } %>
<%= f.simple_fields_for :forum_post do |fa| %>
<%= fa.input :creator_name, label: "Forum Poster", input_html: { value: params.dig(:search, :forum_post, :creator_name), data: { autocomplete: "user" } } %>
<% end %>
<%= f.input :forum_post_id, label: "Forum Post", input_html: { value: params[:search][:forum_post_id] } %>
<%= f.input :score, label: "Type", collection: [["Up", "1"], ["Meh", "0"], ["Down", "-1"]], include_blank: true, selected: params[:search][:score] %>
<%= f.input :score, label: "Score", collection: [["+1", "1"], ["0", "0"], ["-1", "-1"]], include_blank: true, selected: params[:search][:score] %>
<%= f.submit "Search" %>
<% end %>
@@ -12,8 +15,10 @@
<tr>
<th>Forum Post</th>
<th>Forum Topic</th>
<th>Type</th>
<th>Created</th>
<th>Score</th>
<th>Forum Poster</th>
<th>Voter</th>
<th></th>
</tr>
</thead>
<tbody>
@@ -27,12 +32,22 @@
<%= link_to forum_post_vote.forum_post.topic.title, forum_post_vote.forum_post.topic %>
</td>
<td>
<%= forum_post_vote.vote_type %>
<%= link_to sprintf("%+d", forum_post_vote.score), forum_post_votes_path(search: { score: forum_post_vote.score }) %>
</td>
<td>
<%= link_to_user forum_post_vote.forum_post.creator %>
<%= link_to "»", forum_post_votes_path(search: { forum_post: { creator_name: forum_post_vote.forum_post.creator.name }}) %>
<div><%= time_ago_in_words_tagged(forum_post_vote.forum_post.created_at) %></div>
</td>
<td>
<%= link_to_user forum_post_vote.creator %>
<%= link_to "»", forum_post_votes_path(search: { creator_name: forum_post_vote.creator.name }) %>
<p><%= time_ago_in_words_tagged(forum_post_vote.updated_at) %></p>
<div><%= time_ago_in_words_tagged(forum_post_vote.created_at) %></div>
</td>
<td>
<% if forum_post_vote.creator == CurrentUser.user %>
<%= link_to "unvote", forum_post_vote_path(forum_post_vote, format: "js"), remote: true, method: :delete %>
<% end %>
</td>
</tr>
<% end %>

View File

@@ -1,7 +1,7 @@
<div id="c-post-votes">
<div id="a-index">
<%= search_form_for(post_votes_path) do |f| %>
<%= f.input :user_name, label: "User", input_html: { value: params[:search][:user_name], "data-autocomplete": "user" } %>
<%= f.input :user_name, label: "Voter", input_html: { value: params[:search][:user_name], "data-autocomplete": "user" } %>
<%= f.input :post_id, label: "Post", input_html: { value: params[:search][:post_id] } %>
<%= f.input :post_tags_match, label: "Tags", input_html: { value: params[:search][:post_tags_match], "data-autocomplete": "tag-query" } %>
<%= f.input :score, collection: [["+3", "3"], ["+1", "1"], ["-1", "-1"], ["-3", "-3"]], include_blank: true, selected: params[:search][:score] %>
@@ -14,7 +14,8 @@
<th>Post</th>
<th>Tags</th>
<th>Score</th>
<th>Created</th>
<th>Uploader</th>
<th>Voter</th>
<th></th>
</tr>
</thead>
@@ -28,13 +29,20 @@
<%= TagSetPresenter.new(vote.post.tag_array).inline_tag_list_html %>
</td>
<td><%= link_to sprintf("%+d", vote.score), post_votes_path(search: { score: vote.score }) %></td>
<td>
<%= link_to_user vote.post.uploader %>
<%= link_to "»", post_votes_path(search: { post_tags_match: "user:#{vote.post.uploader.name}" }) %>
<div><%= time_ago_in_words_tagged(vote.post.created_at) %></div>
</td>
<td>
<%= link_to_user vote.user %>
<%= link_to "»", post_votes_path(search: { user_name: vote.user.name }) %>
<div><%= time_ago_in_words_tagged(vote.created_at) %></div>
</td>
<td>
<%= link_to "undo", post_post_votes_path(vote.post), remote: true, method: :delete %>
<% if vote.user == CurrentUser.user %>
<%= link_to "unvote", post_post_votes_path(vote.post), remote: true, method: :delete %>
<% end %>
</td>
</tr>
<% end %>

View File

@@ -87,9 +87,11 @@
<% if CurrentUser.user == user || CurrentUser.user.is_admin? %>
<tr>
<th>Post Votes</th>
<th>Votes</th>
<td>
<%= link_to user.post_votes.count, post_votes_path(search: { user_name: user.name }) %>
<%= link_to user.post_votes.count, post_votes_path(search: { user_name: user.name }) %> posts,
<%= link_to user.comment_votes.count, comment_votes_path(search: { user_name: user.name }) %> comments,
<%= link_to user.forum_post_votes.count, forum_post_votes_path(search: { creator_name: user.name }) %> forum posts
</td>
</tr>
<% end %>