From f4376fc7a3c5ae14fb0a4d34bfc4408ff8d0b5d0 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 28 Oct 2019 03:21:18 -0500 Subject: [PATCH] post votes: add index page. --- app/controllers/post_votes_controller.rb | 9 +++- app/models/post_vote.rb | 12 +++++ .../comments/partials/index/_header.html.erb | 2 +- app/views/post_votes/index.html.erb | 46 +++++++++++++++++++ .../posts/partials/show/_information.html.erb | 6 +-- app/views/static/site_map.html.erb | 3 +- config/routes.rb | 5 +- test/factories/post_vote.rb | 7 +++ test/functional/post_votes_controller_test.rb | 19 ++++++-- 9 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 app/views/post_votes/index.html.erb create mode 100644 test/factories/post_vote.rb diff --git a/app/controllers/post_votes_controller.rb b/app/controllers/post_votes_controller.rb index 0331c60ee..f02e1bc4c 100644 --- a/app/controllers/post_votes_controller.rb +++ b/app/controllers/post_votes_controller.rb @@ -1,9 +1,14 @@ class PostVotesController < ApplicationController - before_action :voter_only + before_action :voter_only, only: [:create, :destroy] skip_before_action :api_check - respond_to :js, :json, :xml + respond_to :js, :json, :xml, :html rescue_with PostVote::Error, status: 422 + def index + @post_votes = PostVote.includes(:post, :user).paginated_search(params) + respond_with(@post_votes) + end + def create @post = Post.find(params[:post_id]) @post.vote!(params[:score]) diff --git a/app/models/post_vote.rb b/app/models/post_vote.rb index ffb063940..d5e56c4a8 100644 --- a/app/models/post_vote.rb +++ b/app/models/post_vote.rb @@ -23,6 +23,18 @@ class PostVote < ApplicationRecord select_values_sql("select post_id from post_votes where score > 0 and user_id = ?", user_id) end + def self.visible(user = CurrentUser.user) + return all if user.is_admin? + where(user: user) + end + + def self.search(params) + q = super + q = q.visible + q = q.search_attributes(params, :post, :user, :score) + q.apply_default_order(params) + end + def initialize_attributes self.user_id ||= CurrentUser.user.id diff --git a/app/views/comments/partials/index/_header.html.erb b/app/views/comments/partials/index/_header.html.erb index 735bc541c..d69fff8c1 100644 --- a/app/views/comments/partials/index/_header.html.erb +++ b/app/views/comments/partials/index/_header.html.erb @@ -19,7 +19,7 @@ <%= post.score %> <% if CurrentUser.is_voter? %> - (vote <%= link_to(content_tag("i", nil, class: "far fa-thumbs-up"), post_votes_path(:score => "up", :post_id => post.id), :remote => true, :method => :post) %>/<%= link_to(content_tag("i", nil, class: "far fa-thumbs-down"), post_votes_path(:score => "down", :post_id => post.id), :remote => true, :method => :post) %>) + (vote <%= link_to(content_tag("i", nil, class: "far fa-thumbs-up"), post_post_votes_path(:score => "up", :post_id => post.id), :remote => true, :method => :post) %>/<%= link_to(content_tag("i", nil, class: "far fa-thumbs-down"), post_post_votes_path(:score => "down", :post_id => post.id), :remote => true, :method => :post) %>) <% end %> diff --git a/app/views/post_votes/index.html.erb b/app/views/post_votes/index.html.erb new file mode 100644 index 000000000..db7a7ed5b --- /dev/null +++ b/app/views/post_votes/index.html.erb @@ -0,0 +1,46 @@ +
+
+ <%= 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 :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] %> + <%= f.submit "Search" %> + <% end %> + + + + + + + + + + + + + <% @post_votes.each do |vote| %> + + + + + + + + <% end %> + +
PostTagsScoreCreated
+ <%= PostPresenter.preview(vote.post, show_deleted: true) %> + + <%= 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.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 %> +
+ + <%= numbered_paginator(@post_votes) %> +
+
diff --git a/app/views/posts/partials/show/_information.html.erb b/app/views/posts/partials/show/_information.html.erb index 672710e69..b36643ae4 100644 --- a/app/views/posts/partials/show/_information.html.erb +++ b/app/views/posts/partials/show/_information.html.erb @@ -22,11 +22,11 @@ <% if CurrentUser.is_voter? %> <%= tag.span id: "vote-links-for-post-#{post.id}", style: ("display: none;" if !@post.can_be_voted_by?(CurrentUser.user)) do %> (vote - <%= link_to tag.i(class: "far fa-thumbs-up"), post_votes_path(post_id: post.id, score: "up"), remote: true, method: :post %> - <%= link_to tag.i(class: "far fa-thumbs-down"), post_votes_path(post_id: post.id, score: "down"), remote: true, method: :post %>) + <%= link_to tag.i(class: "far fa-thumbs-up"), post_post_votes_path(post_id: post.id, score: "up"), remote: true, method: :post %> + <%= link_to tag.i(class: "far fa-thumbs-down"), post_post_votes_path(post_id: post.id, score: "down"), remote: true, method: :post %>) <% end %> <%= tag.span id: "unvote-link-for-post-#{post.id}", style: ("display: none;" if @post.can_be_voted_by?(CurrentUser.user)) do %> - (<%= link_to "undo vote", post_votes_path(post), remote: true, method: :delete, class: "unvote-post-link" %>) + (<%= link_to "undo vote", post_post_votes_path(post), remote: true, method: :delete, class: "unvote-post-link" %>) <% end %> <% end %> diff --git a/app/views/static/site_map.html.erb b/app/views/static/site_map.html.erb index e33c610f5..872ddae27 100644 --- a/app/views/static/site_map.html.erb +++ b/app/views/static/site_map.html.erb @@ -9,13 +9,13 @@
  • <%= link_to("Upload Listing", uploads_path) %>
  • <%= link_to("Popular", popular_explore_posts_path) %>
  • <%= link_to("Most Viewed", viewed_explore_posts_path) %>
  • +
  • <%= link_to("Votes", post_votes_path) %>
  • <% if CurrentUser.can_approve_posts? %>
  • <%= link_to("Moderate", moderator_post_queue_path) %>
  • <% end %> <% if CurrentUser.is_moderator? %>
  • <%= link_to("Mass Edit", edit_moderator_tag_path) %>
  • <% end %> -
  • <%= link_to("Similar Images Search", iqdb_queries_path) %>