diff --git a/app/controllers/post_approvals_controller.rb b/app/controllers/post_approvals_controller.rb new file mode 100644 index 000000000..ed2262e24 --- /dev/null +++ b/app/controllers/post_approvals_controller.rb @@ -0,0 +1,8 @@ +class PostApprovalsController < ApplicationController + respond_to :html, :xml, :json + + def index + @post_approvals = PostApproval.includes(:post, :user).search(search_params).paginate(params[:page], limit: params[:limit]) + respond_with(@post_approvals) + end +end diff --git a/app/models/post_approval.rb b/app/models/post_approval.rb index e93c99077..ce1e5d7f5 100644 --- a/app/models/post_approval.rb +++ b/app/models/post_approval.rb @@ -5,10 +5,6 @@ class PostApproval < ApplicationRecord validate :validate_approval after_create :approve_post - def self.prune! - where("created_at < ?", 1.month.ago).delete_all - end - def validate_approval if post.is_status_locked? errors.add(:post, "is locked and cannot be approved") @@ -33,4 +29,26 @@ class PostApproval < ApplicationRecord post.flags.each(&:resolve!) post.update(approver: user, is_flagged: false, is_pending: false, is_deleted: false) end + + concerning :SearchMethods do + class_methods do + def post_tags_match(query) + PostQueryBuilder.new(query).build(self.joins(:post)) + end + + def search(params) + q = super + params[:user_id] = User.name_to_id(params[:user_name]) if params[:user_name] + + if params[:post_tags_match].present? + q = q.post_tags_match(params[:post_tags_match]) + end + + q = q.attribute_matches(:user_id, params[:user_id]) + q = q.attribute_matches(:post_id, params[:post_id]) + + q.apply_default_order(params) + end + end + end end diff --git a/app/views/post_approvals/index.html.erb b/app/views/post_approvals/index.html.erb new file mode 100644 index 000000000..1c7d2e13b --- /dev/null +++ b/app/views/post_approvals/index.html.erb @@ -0,0 +1,42 @@ +
+
+

Approvals

+ <%= render "posts/partials/common/inline_blacklist" %> + + <%= simple_form_for(:search, url: post_approvals_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> + <%= f.input :user_name, label: "Approver", input_html: { value: params[:search][:user_name] } %> + <%= f.input :post_tags_match, label: "Tags", input_html: { value: params[:search][:post_tags_match], data: { autocomplete: "tag-query" } } %> + <%= f.submit "Search" %> + <% end %> + + + + + + + + + + <% @post_approvals.each do |post_approval| %> + + + + + + <% end %> + +
PostApprover
+ <%= PostPresenter.preview(post_approval.post, :tags => "status:any") %> + + <%= link_to_user post_approval.user %> + <%= link_to "ยป", post_approvals_path(search: params[:search].merge(user_name: post_approval.user.name)) %> +
<%= time_ago_in_words_tagged post_approval.created_at %> +
+ + <%= numbered_paginator(@post_approvals) %> +
+
+ +<% content_for(:page_title) do %> + Approvals - <%= Danbooru.config.app_name %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 2c67a6c38..bb31bb3ce 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -220,6 +220,7 @@ Rails.application.routes.draw do end resources :post_appeals resources :post_flags + resources :post_approvals, only: [:index] resources :post_versions, :only => [:index, :search] do member do put :undo diff --git a/test/factories/post_approval.rb b/test/factories/post_approval.rb new file mode 100644 index 000000000..6f3eaf824 --- /dev/null +++ b/test/factories/post_approval.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory(:post_approval) do + user factory: :moderator_user + post factory: :post, is_pending: true + end +end diff --git a/test/functional/post_approvals_controller_test.rb b/test/functional/post_approvals_controller_test.rb new file mode 100644 index 000000000..55caec659 --- /dev/null +++ b/test/functional/post_approvals_controller_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' + +class PostApprovalsControllerTest < ActionDispatch::IntegrationTest + context "The post approvals controller" do + setup do + @approval = FactoryBot.create(:post_approval) + end + + context "index action" do + should "render" do + get post_approvals_path + assert_response :success + end + end + end +end diff --git a/test/unit/post_approval_test.rb b/test/unit/post_approval_test.rb index f6a82ba8b..5453b6e0e 100644 --- a/test/unit/post_approval_test.rb +++ b/test/unit/post_approval_test.rb @@ -7,7 +7,7 @@ class PostApprovalTest < ActiveSupport::TestCase CurrentUser.user = @user CurrentUser.ip_addr = "127.0.0.1" - @post = FactoryBot.create(:post, uploader_id: @user.id, is_pending: true) + @post = FactoryBot.create(:post, uploader_id: @user.id, tag_string: "touhou", is_pending: true) @approver = FactoryBot.create(:user) @approver.can_approve_posts = true @@ -58,5 +58,14 @@ class PostApprovalTest < ActiveSupport::TestCase end end end + + context "#search method" do + should "work" do + @approval = @post.approve!(@approver) + @approvals = PostApproval.search(user_name: @approver.name, post_tags_match: "touhou", post_id: @post.id) + + assert_equal([@approval.id], @approvals.map(&:id)) + end + end end end