From fd4a9d4d3068579c70cab098cc9681f8ea6f6334 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 28 Oct 2019 14:34:56 -0500 Subject: [PATCH] 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. --- app/controllers/post_votes_controller.rb | 4 ++-- app/models/forum_post_vote.rb | 6 +++++ app/models/user.rb | 1 + app/views/comment_votes/index.html.erb | 4 +++- app/views/forum_post_votes/index.html.erb | 27 ++++++++++++++++++----- app/views/post_votes/index.html.erb | 14 +++++++++--- app/views/users/_statistics.html.erb | 6 +++-- 7 files changed, 48 insertions(+), 14 deletions(-) diff --git a/app/controllers/post_votes_controller.rb b/app/controllers/post_votes_controller.rb index f02e1bc4c..87009f197 100644 --- a/app/controllers/post_votes_controller.rb +++ b/app/controllers/post_votes_controller.rb @@ -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 diff --git a/app/models/forum_post_vote.rb b/app/models/forum_post_vote.rb index d2780680c..9637aca8f 100644 --- a/app/models/forum_post_vote.rb +++ b/app/models/forum_post_vote.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 8607e4a88..117fc3a98 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/comment_votes/index.html.erb b/app/views/comment_votes/index.html.erb index a0210c550..d6f9e290c 100644 --- a/app/views/comment_votes/index.html.erb +++ b/app/views/comment_votes/index.html.erb @@ -44,7 +44,9 @@
<%= time_ago_in_words_tagged(vote.created_at) %>
- <%= 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 %> <% end %> diff --git a/app/views/forum_post_votes/index.html.erb b/app/views/forum_post_votes/index.html.erb index 5391fb6b3..51d732ca3 100644 --- a/app/views/forum_post_votes/index.html.erb +++ b/app/views/forum_post_votes/index.html.erb @@ -1,9 +1,12 @@
<%= 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 @@ Forum Post Forum Topic - Type - Created + Score + Forum Poster + Voter + @@ -27,12 +32,22 @@ <%= link_to forum_post_vote.forum_post.topic.title, forum_post_vote.forum_post.topic %> - <%= forum_post_vote.vote_type %> + <%= link_to sprintf("%+d", forum_post_vote.score), forum_post_votes_path(search: { score: forum_post_vote.score }) %> + + + <%= 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 }}) %> +
<%= time_ago_in_words_tagged(forum_post_vote.forum_post.created_at) %>
<%= link_to_user forum_post_vote.creator %> <%= link_to "»", forum_post_votes_path(search: { creator_name: forum_post_vote.creator.name }) %> -

<%= time_ago_in_words_tagged(forum_post_vote.updated_at) %>

+
<%= time_ago_in_words_tagged(forum_post_vote.created_at) %>
+ + + <% if forum_post_vote.creator == CurrentUser.user %> + <%= link_to "unvote", forum_post_vote_path(forum_post_vote, format: "js"), remote: true, method: :delete %> + <% end %> <% end %> diff --git a/app/views/post_votes/index.html.erb b/app/views/post_votes/index.html.erb index db7a7ed5b..dd0f9ba48 100644 --- a/app/views/post_votes/index.html.erb +++ b/app/views/post_votes/index.html.erb @@ -1,7 +1,7 @@
<%= 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 @@ Post Tags Score - Created + Uploader + Voter @@ -28,13 +29,20 @@ <%= TagSetPresenter.new(vote.post.tag_array).inline_tag_list_html %> <%= link_to sprintf("%+d", vote.score), post_votes_path(search: { score: vote.score }) %> + + <%= link_to_user vote.post.uploader %> + <%= link_to "»", post_votes_path(search: { post_tags_match: "user:#{vote.post.uploader.name}" }) %> +
<%= time_ago_in_words_tagged(vote.post.created_at) %>
+ <%= link_to_user vote.user %> <%= link_to "»", post_votes_path(search: { user_name: vote.user.name }) %>
<%= time_ago_in_words_tagged(vote.created_at) %>
- <%= 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 %> <% end %> diff --git a/app/views/users/_statistics.html.erb b/app/views/users/_statistics.html.erb index 1416c5129..f58130f16 100644 --- a/app/views/users/_statistics.html.erb +++ b/app/views/users/_statistics.html.erb @@ -87,9 +87,11 @@ <% if CurrentUser.user == user || CurrentUser.user.is_admin? %> - Post Votes + Votes - <%= 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 <% end %>