post votes: add index page.

This commit is contained in:
evazion
2019-10-28 03:21:18 -05:00
parent 754e3a2ee2
commit f4376fc7a3
9 changed files with 95 additions and 14 deletions

View File

@@ -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])

View File

@@ -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

View File

@@ -19,7 +19,7 @@
<span>
<span id="score-for-post-<%= post.id %>"><%= post.score %></span>
<% 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 %>
</span>
</span>

View File

@@ -0,0 +1,46 @@
<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 :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 %>
<table class="striped autofit">
<thead>
<tr>
<th>Post</th>
<th>Tags</th>
<th>Score</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
<% @post_votes.each do |vote| %>
<tr>
<td>
<%= PostPresenter.preview(vote.post, show_deleted: true) %>
</td>
<td class="col-expand">
<%= 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.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 %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= numbered_paginator(@post_votes) %>
</div>
</div>

View File

@@ -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 %>
</li>

View File

@@ -9,13 +9,13 @@
<li><%= link_to("Upload Listing", uploads_path) %></li>
<li><%= link_to("Popular", popular_explore_posts_path) %></li>
<li><%= link_to("Most Viewed", viewed_explore_posts_path) %></li>
<li><%= link_to("Votes", post_votes_path) %></li>
<% if CurrentUser.can_approve_posts? %>
<li><%= link_to("Moderate", moderator_post_queue_path) %></li>
<% end %>
<% if CurrentUser.is_moderator? %>
<li><%= link_to("Mass Edit", edit_moderator_tag_path) %></li>
<% end %>
<li><%= link_to("Similar Images Search", iqdb_queries_path) %></li>
</ul>
<ul>
<li><h1>Post Events</h1></li>
@@ -36,6 +36,7 @@
<li><%= link_to("Bookmarklet", bookmarklet_path) %></li>
<li><%= link_to("User Scripts", wiki_pages_path(title: "about:userscripts")) %></li>
<li><%= link_to("API Documentation", wiki_pages_path(:title => "help:api")) %></li>
<li><%= link_to("Similar Images Search", iqdb_queries_path) %></li>
</ul>
<ul>
<li><h1>Artists</h1></li>

View File

@@ -202,14 +202,15 @@ Rails.application.routes.draw do
end
end
resources :post_replacements, :only => [:index, :new, :create, :update]
resources :posts, :only => [:index, :show, :update] do
resources :post_votes, only: [:index]
resources :posts, only: [:index, :show, :update] do
resources :events, :only => [:index], :controller => "post_events"
resources :replacements, :only => [:index, :new, :create], :controller => "post_replacements"
resource :artist_commentary, :only => [:index, :show] do
collection { put :create_or_update }
member { put :revert }
end
resource :votes, :controller => "post_votes", :only => [:create, :destroy]
resource :votes, controller: "post_votes", only: [:create, :destroy], as: "post_votes"
collection do
get :random
end

View File

@@ -0,0 +1,7 @@
FactoryBot.define do
factory(:post_vote) do
user
post
score { [-1, 1].sample }
end
end

View File

@@ -9,9 +9,18 @@ class PostVotesControllerTest < ActionDispatch::IntegrationTest
end
end
context "index action" do
should "work" do
as(@user) { create(:post_vote, post_id: @post.id, user_id: @user.id) }
get post_votes_path
assert_response :success
end
end
context "create action" do
should "not allow anonymous users to vote" do
post post_votes_path(post_id: @post.id), params: {:score => "up", :format => "js"}
post post_post_votes_path(post_id: @post.id), params: {:score => "up", :format => "js"}
assert_response 403
assert_equal(0, @post.reload.score)
end
@@ -19,20 +28,20 @@ class PostVotesControllerTest < ActionDispatch::IntegrationTest
should "not allow banned users to vote" do
@banned = create(:user)
@ban = create(:ban, user: @banned)
post_auth post_votes_path(post_id: @post.id), @banned, params: {:score => "up", :format => "js"}
post_auth post_post_votes_path(post_id: @post.id), @banned, params: {:score => "up", :format => "js"}
assert_response 403
assert_equal(0, @post.reload.score)
end
should "not allow members to vote" do
@member = create(:member_user)
post_auth post_votes_path(post_id: @post.id), @member, params: {:score => "up", :format => "js"}
post_auth post_post_votes_path(post_id: @post.id), @member, params: {:score => "up", :format => "js"}
assert_response 403
assert_equal(0, @post.reload.score)
end
should "increment a post's score if the score is positive" do
post_auth post_votes_path(post_id: @post.id), @user, params: {:score => "up", :format => "js"}
post_auth post_post_votes_path(post_id: @post.id), @user, params: {:score => "up", :format => "js"}
assert_response :success
@post.reload
assert_equal(1, @post.score)
@@ -47,7 +56,7 @@ class PostVotesControllerTest < ActionDispatch::IntegrationTest
should "fail silently on an error" do
assert_nothing_raised do
post_auth post_votes_path(post_id: @post.id), @user, params: {:score => "up", :format => "js"}
post_auth post_post_votes_path(post_id: @post.id), @user, params: {:score => "up", :format => "js"}
end
end
end