From 39bd766b343feae8ca52364ee299aaf22b5c0e21 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 2 Aug 2019 21:22:33 -0500 Subject: [PATCH] Fix #4053: Add disapproval index improvements. Add search form to /moderator/post/disapprovals. --- .../moderator/post/disapprovals_controller.rb | 5 +-- app/models/post_disapproval.rb | 34 +++++++++++++++++++ .../post/disapprovals/index.html.erb | 34 ++++++++++++++++--- .../post/disapprovals_controller_test.rb | 11 ++++++ test/unit/post_disapproval_test.rb | 13 ++++++- 5 files changed, 89 insertions(+), 8 deletions(-) diff --git a/app/controllers/moderator/post/disapprovals_controller.rb b/app/controllers/moderator/post/disapprovals_controller.rb index e8838d60b..aef11f095 100644 --- a/app/controllers/moderator/post/disapprovals_controller.rb +++ b/app/controllers/moderator/post/disapprovals_controller.rb @@ -3,7 +3,7 @@ module Moderator class DisapprovalsController < ApplicationController before_action :approver_only skip_before_action :api_check - respond_to :js, :json, :xml + respond_to :js, :html, :json, :xml def create cookies.permanent[:moderated] = Time.now.to_i @@ -12,7 +12,8 @@ module Moderator end def index - @post_disapprovals = PostDisapproval.paginate(params[:page]) + @post_disapprovals = PostDisapproval.includes(:user).search(search_params).paginate(params[:page], limit: params[:limit]) + respond_with(@post_disapprovals) end private diff --git a/app/models/post_disapproval.rb b/app/models/post_disapproval.rb index 4141abd4e..d0060cb2f 100644 --- a/app/models/post_disapproval.rb +++ b/app/models/post_disapproval.rb @@ -8,6 +8,7 @@ class PostDisapproval < ApplicationRecord validates_inclusion_of :reason, :in => %w(legacy breaks_rules poor_quality disinterest) scope :with_message, -> {where("message is not null and message <> ''")} + scope :without_message, -> {where("message is null or message = ''")} scope :breaks_rules, -> {where(:reason => "breaks_rules")} scope :poor_quality, -> {where(:reason => "poor_quality")} scope :disinterest, -> {where(:reason => ["disinterest", "legacy"])} @@ -43,4 +44,37 @@ class PostDisapproval < ApplicationRecord PostVote.create(:score => -1, :post_id => post_id) end end + + concerning :SearchMethods do + class_methods do + def post_tags_match(query) + where(post_id: PostQueryBuilder.new(query).build.reorder("")) + end + + def search(params) + q = super + + q = q.attribute_matches(:post_id, params[:post_id]) + q = q.attribute_matches(:user_id, params[:user_id]) + q = q.attribute_matches(:message, params[:message_matches]) + q = q.search_text_attribute(:message, params) + + q = q.post_tags_match(params[:post_tags_match]) if params[:post_tags_match].present? + q = q.where(user_id: User.search(name_matches: params[:creator_name])) if params[:creator_name].present? + q = q.where(reason: params[:reason]) if params[:reason].present? + + q = q.with_message if params[:has_message].to_s.truthy? + q = q.without_message if params[:has_message].to_s.falsy? + + case params[:order] + when "post_id", "post_id_desc" + q = q.order(post_id: :desc, id: :desc) + else + q = q.apply_default_order(params) + end + + q.apply_default_order(params) + end + end + end end diff --git a/app/views/moderator/post/disapprovals/index.html.erb b/app/views/moderator/post/disapprovals/index.html.erb index 3f240668a..c28f0fca7 100644 --- a/app/views/moderator/post/disapprovals/index.html.erb +++ b/app/views/moderator/post/disapprovals/index.html.erb @@ -2,20 +2,44 @@

Disapprovals

- + <%= simple_form_for(:search, url: moderator_post_disapprovals_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> + <%= f.input :creator_name, label: "Creator", input_html: { value: params[:search][:creator_name] } %> + <%= f.input :post_id, label: "Post ID", 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 :message_matches, label: "Message", input_html: { value: params[:search][:message_matches] } %> + <%= f.input :reason, label: "Reason", collection: %w[breaks_rules disinterest poor_quality].map { |x| [x.humanize, x] }, include_blank: true, selected: params[:search][:reason] %> + <%= f.input :has_message, label: "Has Message?", collection: %w[Yes No], include_blank: true, selected: params[:search][:has_message] %> + <%= f.input :order, collection: [["ID", "id_desc"], ["Post ID", "post_id_desc"]], selected: params[:search][:order] %> + <%= f.submit "Search" %> + <% end %> + +
+ - + <% @post_disapprovals.each do |post_disapproval| %> - - - + + + + <% end %> diff --git a/test/functional/moderator/post/disapprovals_controller_test.rb b/test/functional/moderator/post/disapprovals_controller_test.rb index 4dcdc991d..6c8c0c718 100644 --- a/test/functional/moderator/post/disapprovals_controller_test.rb +++ b/test/functional/moderator/post/disapprovals_controller_test.rb @@ -9,6 +9,8 @@ module Moderator as_user do @post = create(:post, :is_pending => true) end + + CurrentUser.user = @admin end context "create action" do @@ -28,6 +30,15 @@ module Moderator end end end + + context "index action" do + should "render" do + disapproval = FactoryBot.create(:post_disapproval, post: @post) + get_auth moderator_post_disapprovals_path, @admin + + assert_response :success + end + end end end end diff --git a/test/unit/post_disapproval_test.rb b/test/unit/post_disapproval_test.rb index 9bbec4f2a..af2248e75 100644 --- a/test/unit/post_disapproval_test.rb +++ b/test/unit/post_disapproval_test.rb @@ -3,7 +3,7 @@ require 'test_helper' class PostDisapprovalTest < ActiveSupport::TestCase context "In all cases" do setup do - @alice = FactoryBot.create(:moderator_user) + @alice = FactoryBot.create(:moderator_user, name: "alice") CurrentUser.user = @alice CurrentUser.ip_addr = "127.0.0.1" end @@ -90,6 +90,17 @@ class PostDisapprovalTest < ActiveSupport::TestCase assert(@uploaders[1].dmails.exists?(from: bot, to: @uploaders[1])) end end + + context "#search" do + should "work" do + disapproval1 = FactoryBot.create(:post_disapproval, user: @alice, post: @post_1, reason: "breaks_rules") + disapproval2 = FactoryBot.create(:post_disapproval, user: @alice, post: @post_2, reason: "poor_quality", message: "bad anatomy") + + assert_equal([disapproval1.id], PostDisapproval.search(reason: "breaks_rules").pluck(:id)) + assert_equal([disapproval2.id], PostDisapproval.search(message: "bad anatomy").pluck(:id)) + assert_equal([disapproval2.id, disapproval1.id], PostDisapproval.search(creator_name: "alice").pluck(:id)) + end + end end end end
PostMessage ReasonCreatorCreated
<%= link_to post_disapproval.post_id, post_path(post_disapproval.post_id) %><%= post_disapproval.reason %>: <%= post_disapproval.message %><%= post_disapproval.user.name %> + <%= link_to "post ##{post_disapproval.post_id}", post_path(post_disapproval.post_id) %> + <%= link_to "»", moderator_post_disapprovals_path(search: params[:search].merge(post_id: post_disapproval.post_id)) %> + <%= format_text(post_disapproval.message) %> + <%= link_to post_disapproval.reason.humanize, moderator_post_disapprovals_path(search: params[:search].merge(reason: post_disapproval.reason)) %> + + <%= link_to_user post_disapproval.user %> + <%= link_to "»", moderator_post_disapprovals_path(search: params[:search].merge(creator_name: post_disapproval.user&.name)) %> +

+ <%= compact_time(post_disapproval.updated_at) %> +

+