Skip to content

Commit

Permalink
Fix #4053: Add disapproval index improvements.
Browse files Browse the repository at this point in the history
Add search form to /moderator/post/disapprovals.
  • Loading branch information
evazion committed Aug 3, 2019
1 parent 6c69165 commit 39bd766
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
5 changes: 3 additions & 2 deletions app/controllers/moderator/post/disapprovals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Post
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
Expand All @@ -12,7 +12,8 @@ def create
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
Expand Down
34 changes: 34 additions & 0 deletions app/models/post_disapproval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"])}
Expand Down Expand Up @@ -43,4 +44,37 @@ def create_downvote
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
34 changes: 29 additions & 5 deletions app/views/moderator/post/disapprovals/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,44 @@
<div id="a-index">
<h1>Disapprovals</h1>

<table class="striped" width="100%">
<%= 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 %>

<table class="striped autofit" width="100%">
<thead>
<tr>
<th>Post</th>
<th>Message</th>
<th>Reason</th>
<th>Creator</th>
<th>Created</th>
</tr>
</thead>
<tbody>
<% @post_disapprovals.each do |post_disapproval| %>
<tr>
<td><%= link_to post_disapproval.post_id, post_path(post_disapproval.post_id) %></td>
<td><%= post_disapproval.reason %>: <%= post_disapproval.message %></td>
<td><%= post_disapproval.user.name %></td>
<td>
<%= 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)) %>
</td>
<td class="col-expand"><%= format_text(post_disapproval.message) %></td>
<td>
<%= link_to post_disapproval.reason.humanize, moderator_post_disapprovals_path(search: params[:search].merge(reason: post_disapproval.reason)) %>
</td>
<td>
<%= link_to_user post_disapproval.user %>
<%= link_to "»", moderator_post_disapprovals_path(search: params[:search].merge(creator_name: post_disapproval.user&.name)) %>
<p>
<%= compact_time(post_disapproval.updated_at) %>
</p>
</td>
</tr>
<% end %>
</tbody>
Expand Down
11 changes: 11 additions & 0 deletions test/functional/moderator/post/disapprovals_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class DisapprovalsControllerTest < ActionDispatch::IntegrationTest
as_user do
@post = create(:post, :is_pending => true)
end

CurrentUser.user = @admin
end

context "create action" do
Expand All @@ -28,6 +30,15 @@ class DisapprovalsControllerTest < ActionDispatch::IntegrationTest
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
Expand Down
13 changes: 12 additions & 1 deletion test/unit/post_disapproval_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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
Expand Down Expand Up @@ -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

0 comments on commit 39bd766

Please sign in to comment.