diff --git a/app/controllers/forum_post_votes_controller.rb b/app/controllers/forum_post_votes_controller.rb
index f84653cd7..b0829f37f 100644
--- a/app/controllers/forum_post_votes_controller.rb
+++ b/app/controllers/forum_post_votes_controller.rb
@@ -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
diff --git a/app/models/forum_post_vote.rb b/app/models/forum_post_vote.rb
index 4a987e2bf..d2780680c 100644
--- a/app/models/forum_post_vote.rb
+++ b/app/models/forum_post_vote.rb
@@ -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
diff --git a/app/models/user.rb b/app/models/user.rb
index cd4b15ef2..8607e4a88 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -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
diff --git a/app/views/forum_post_votes/_vote.html.erb b/app/views/forum_post_votes/_vote.html.erb
index f5b9d9d23..d46d56ab3 100644
--- a/app/views/forum_post_votes/_vote.html.erb
+++ b/app/views/forum_post_votes/_vote.html.erb
@@ -5,7 +5,7 @@
<% 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}") %>
diff --git a/app/views/forum_post_votes/destroy.js.erb b/app/views/forum_post_votes/destroy.js.erb
index 906bd0807..852f05698 100644
--- a/app/views/forum_post_votes/destroy.js.erb
+++ b/app/views/forum_post_votes/destroy.js.erb
@@ -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);
diff --git a/app/views/forum_post_votes/index.html.erb b/app/views/forum_post_votes/index.html.erb
new file mode 100644
index 000000000..5391fb6b3
--- /dev/null
+++ b/app/views/forum_post_votes/index.html.erb
@@ -0,0 +1,44 @@
+
+
+ <%= 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 %>
+
+
+
+
+ | Forum Post |
+ Forum Topic |
+ Type |
+ Created |
+
+
+
+ <% @forum_post_votes.each do |forum_post_vote| %>
+
+ |
+ <%= 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 }) %>
+ |
+
+ <%= link_to forum_post_vote.forum_post.topic.title, forum_post_vote.forum_post.topic %>
+ |
+
+ <%= forum_post_vote.vote_type %>
+ |
+
+ <%= 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) %>
+ |
+
+ <% end %>
+
+
+
+ <%= numbered_paginator(@forum_post_votes) %>
+
+
diff --git a/app/views/static/site_map.html.erb b/app/views/static/site_map.html.erb
index 7230f56f4..e33c610f5 100644
--- a/app/views/static/site_map.html.erb
+++ b/app/views/static/site_map.html.erb
@@ -94,6 +94,7 @@
<%= link_to("Help", wiki_pages_path(:title => "help:forum")) %>
<%= link_to("Listing", forum_topics_path) %>
<%= link_to("Search", search_forum_posts_path) %>
+ <%= link_to("Votes", forum_post_votes_path) %>
<%= link_to("RSS", forum_topics_path(:atom)) %>
diff --git a/config/routes.rb b/config/routes.rb
index 720c86c26..67c7f21a7 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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
diff --git a/test/factories/forum_post_vote.rb b/test/factories/forum_post_vote.rb
index c745c73fd..3848d0db8 100644
--- a/test/factories/forum_post_vote.rb
+++ b/test/factories/forum_post_vote.rb
@@ -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
diff --git a/test/functional/forum_post_votes_controller_test.rb b/test/functional/forum_post_votes_controller_test.rb
new file mode 100644
index 000000000..635345faa
--- /dev/null
+++ b/test/functional/forum_post_votes_controller_test.rb
@@ -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