forum post votes: add index page.

This commit is contained in:
evazion
2019-10-27 23:05:48 -05:00
parent d617b20b49
commit 0e159960a2
10 changed files with 92 additions and 19 deletions

View File

@@ -1,32 +1,27 @@
class ForumPostVotesController < ApplicationController
respond_to :js
before_action :load_forum_post
before_action :load_vote, only: [:destroy]
before_action :member_only
respond_to :html, :xml, :json, :js
before_action :member_only, only: [:create, :destroy]
def index
@forum_post_votes = ForumPostVote.includes(creator: [], forum_post: [:topic]).paginated_search(params)
respond_with(@forum_post_votes)
end
def create
@forum_post = ForumPost.find(params[:forum_post_id])
@forum_post_vote = @forum_post.votes.create(forum_post_vote_params)
respond_with(@forum_post_vote)
end
def destroy
@forum_post_vote = CurrentUser.user.forum_post_votes.find(params[:id])
@forum_post_vote.destroy
respond_with(@forum_post_vote)
end
private
def load_vote
@forum_post_vote = @forum_post.votes.where(creator_id: CurrentUser.id).first
raise ActiveRecord::RecordNotFound.new if @forum_post_vote.nil?
end
def load_forum_post
@forum_post = ForumPost.find(params[:forum_post_id])
end
def forum_post_vote_params
params.fetch(:forum_post_vote, {}).permit(:score)
end
end

View File

@@ -8,6 +8,12 @@ class ForumPostVote < ApplicationRecord
scope :by, ->(user_id) {where(creator_id: user_id)}
scope :excluding_user, ->(user_id) {where("creator_id <> ?", user_id)}
def self.search(params)
q = super
q = q.search_attributes(params, :creator, :forum_post_id, :score)
q.apply_default_order(params)
end
def up?
score == 1
end

View File

@@ -91,6 +91,7 @@ class User < ApplicationRecord
has_many :comments, foreign_key: :creator_id
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
has_many :posts, :foreign_key => "uploader_id"
has_many :post_appeals, foreign_key: :creator_id
has_many :post_approvals, :dependent => :destroy

View File

@@ -5,7 +5,7 @@
<li class="vote-score-<%= vote.vote_type %>">
<% if forum_post.tag_change_request && forum_post.tag_change_request.is_pending? && vote.creator_id == CurrentUser.id %>
<%= link_to content_tag(:i, nil, class: "far #{vote.fa_class}"), forum_post_votes_path(forum_post_id: forum_post.id, format: "js"), remote: true, method: :delete %>
<%= link_to content_tag(:i, nil, class: "far #{vote.fa_class}"), forum_post_vote_path(vote, format: "js"), remote: true, method: :delete %>
<%= link_to_user vote.creator %>
<% else %>
<%= content_tag(:i, nil, class: "far #{vote.fa_class}") %>

View File

@@ -1,3 +1,3 @@
Danbooru.notice("Unvoted");
var code = <%= raw render(partial: "forum_post_votes/list", locals: {forum_post: @forum_post, votes: @forum_post.votes}).to_json %>;
$("#forum-post-votes-for-<%= @forum_post.id %>").html(code);
var code = <%= raw render(partial: "forum_post_votes/list", locals: {forum_post: @forum_post_vote.forum_post, votes: @forum_post_vote.forum_post.votes}).to_json %>;
$("#forum-post-votes-for-<%= @forum_post_vote.forum_post_id %>").html(code);

View File

@@ -0,0 +1,44 @@
<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 :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.submit "Search" %>
<% end %>
<table class="striped autofit">
<thead>
<tr>
<th>Forum Post</th>
<th>Forum Topic</th>
<th>Type</th>
<th>Created</th>
</tr>
</thead>
<tbody>
<% @forum_post_votes.each do |forum_post_vote| %>
<tr>
<td>
<%= link_to "Forum ##{forum_post_vote.forum_post_id}", forum_post_vote.forum_post %>
<%= link_to "»", forum_post_votes_path(search: { forum_post_id: forum_post_vote.forum_post_id }) %>
</td>
<td class="col-expand">
<%= link_to forum_post_vote.forum_post.topic.title, forum_post_vote.forum_post.topic %>
</td>
<td>
<%= forum_post_vote.vote_type %>
</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>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= numbered_paginator(@forum_post_votes) %>
</div>
</div>

View File

@@ -94,6 +94,7 @@
<li><%= link_to("Help", wiki_pages_path(:title => "help:forum")) %></li>
<li><%= link_to("Listing", forum_topics_path) %></li>
<li><%= link_to("Search", search_forum_posts_path) %></li>
<li><%= link_to("Votes", forum_post_votes_path) %></li>
<li><%= link_to("RSS", forum_topics_path(:atom)) %></li>
</ul>
<ul>

View File

@@ -132,7 +132,6 @@ Rails.application.routes.draw do
resource :order, :only => [:edit], :controller => "favorite_group_orders"
end
resources :forum_posts do
resource :votes, controller: "forum_post_votes"
member do
post :undelete
end
@@ -140,6 +139,7 @@ Rails.application.routes.draw do
get :search
end
end
resources :forum_post_votes, only: [:index, :create, :destroy]
resources :forum_topics do
member do
post :undelete

View File

@@ -1,3 +1,7 @@
FactoryBot.define do
factory(:forum_post_vote)
factory(:forum_post_vote) do
creator
forum_post
score { [-1, 0, 1].sample }
end
end

View File

@@ -0,0 +1,22 @@
require 'test_helper'
class ForumPostVotesControllerTest < ActionDispatch::IntegrationTest
context "The forum post votes controller" do
setup do
@user = create(:user)
as(@user) do
@forum_topic = create(:forum_topic)
@forum_post = create(:forum_post, topic: @forum_topic)
@forum_post_vote = create(:forum_post_vote, creator: @user, forum_post: @forum_post)
end
end
context "index action" do
should "render" do
get forum_post_votes_path
assert_response :success
end
end
end
end