add post count estimates for bulk update requests
This commit is contained in:
@@ -78,6 +78,28 @@ class AliasAndImplicationImporter
|
||||
end
|
||||
end
|
||||
|
||||
def estimate_update_count
|
||||
tokens = self.class.tokenize(text)
|
||||
tokens.inject(0) do |sum, token|
|
||||
case token[0]
|
||||
when :create_alias
|
||||
sum + TagAlias.new(antecedent_name: token[1], consequent_name: token[2]).estimate_update_count
|
||||
|
||||
when :create_implication
|
||||
sum + TagImplication.new(antecedent_name: token[1], consequent_name: token[2]).estimate_update_count
|
||||
|
||||
when :mass_update
|
||||
sum + Moderator::TagBatchChange.new(token[1], token[2]).estimate_update_count
|
||||
|
||||
when :change_category
|
||||
sum + Tag.find_by_name(token[1]).try(:post_count) || 0
|
||||
|
||||
else
|
||||
sum + 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse(tokens, approver)
|
||||
|
||||
@@ -20,6 +20,10 @@ module Moderator
|
||||
ModAction.log("processed mass update: #{antecedent} -> #{consequent}",:mass_update)
|
||||
end
|
||||
|
||||
def estimate_update_count
|
||||
PostReadOnly.tag_match(antecedent).count
|
||||
end
|
||||
|
||||
def migrate_posts(normalized_antecedent, normalized_consequent)
|
||||
::Post.tag_match(normalized_antecedent.join(" ")).find_each do |post|
|
||||
post.reload
|
||||
|
||||
@@ -229,4 +229,8 @@ class BulkUpdateRequest < ApplicationRecord
|
||||
def is_rejected?
|
||||
status == "rejected"
|
||||
end
|
||||
|
||||
def estimate_update_count
|
||||
AliasAndImplicationImporter.new(script, nil).estimate_update_count
|
||||
end
|
||||
end
|
||||
|
||||
@@ -192,6 +192,10 @@ class TagRelationship < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def estimate_update_count
|
||||
Post.fast_count(antecedent_name, skip_cache: true)
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
include MessageMethods
|
||||
end
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
<li><strong>Creator</strong> <%= link_to_user @bulk_update_request.user %></li>
|
||||
<li><strong>Date</strong> <%= @bulk_update_request.created_at %></li>
|
||||
<li><strong>Status</strong>: <%= @bulk_update_request.status %></li>
|
||||
<li><strong title="How many posts will be affected">Estimate</strong>: <%= @bulk_update_request.estimate_update_count %></li>
|
||||
|
||||
<% if CurrentUser.is_admin? && @bulk_update_request.is_pending? %>
|
||||
<li><strong>Commands</strong> <%= link_to "Approve", approve_bulk_update_request_path(@bulk_update_request), :method => :post %></li>
|
||||
|
||||
@@ -1,21 +1,5 @@
|
||||
module SavedSearchTestHelper
|
||||
def mock_saved_search_service!
|
||||
mock_sqs_service = Class.new do
|
||||
def initialize
|
||||
@commands = []
|
||||
end
|
||||
|
||||
def commands
|
||||
@commands
|
||||
end
|
||||
|
||||
def send_message(msg)
|
||||
@commands << msg.split(/\n/).first
|
||||
end
|
||||
end
|
||||
|
||||
service = mock_sqs_service.new
|
||||
SavedSearch.stubs(:sqs_service).returns(service)
|
||||
Danbooru.config.stubs(:aws_sqs_saved_search_url).returns("http://localhost:3002")
|
||||
SavedSearch.stubs(:enabled?).returns(true)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -26,6 +26,28 @@ class AliasAndImplicationImporterTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
context "#estimate_update_count" do
|
||||
setup do
|
||||
FactoryBot.create(:post, tag_string: "aaa")
|
||||
FactoryBot.create(:post, tag_string: "bbb")
|
||||
FactoryBot.create(:post, tag_string: "ccc")
|
||||
FactoryBot.create(:post, tag_string: "ddd")
|
||||
FactoryBot.create(:post, tag_string: "eee")
|
||||
|
||||
@script = "create alias aaa -> 000\n" +
|
||||
"create implication bbb -> 111\n" +
|
||||
"remove alias ccc -> 222\n" +
|
||||
"remove implication ddd -> 333\n" +
|
||||
"mass update eee -> 444\n"
|
||||
end
|
||||
|
||||
subject { AliasAndImplicationImporter.new(@script, nil) }
|
||||
|
||||
should "return the correct count" do
|
||||
assert_equal(3, subject.estimate_update_count)
|
||||
end
|
||||
end
|
||||
|
||||
context "given a valid list" do
|
||||
setup do
|
||||
@list = "create alias abc -> def\ncreate implication aaa -> bbb\n"
|
||||
|
||||
@@ -13,6 +13,28 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
|
||||
context "#estimate_update_count" do
|
||||
setup do
|
||||
FactoryBot.create(:post, tag_string: "aaa")
|
||||
FactoryBot.create(:post, tag_string: "bbb")
|
||||
FactoryBot.create(:post, tag_string: "ccc")
|
||||
FactoryBot.create(:post, tag_string: "ddd")
|
||||
FactoryBot.create(:post, tag_string: "eee")
|
||||
|
||||
@script = "create alias aaa -> 000\n" +
|
||||
"create implication bbb -> 111\n" +
|
||||
"remove alias ccc -> 222\n" +
|
||||
"remove implication ddd -> 333\n" +
|
||||
"mass update eee -> 444\n"
|
||||
end
|
||||
|
||||
subject { BulkUpdateRequest.new(script: @script) }
|
||||
|
||||
should "return the correct count" do
|
||||
assert_equal(3, subject.estimate_update_count)
|
||||
end
|
||||
end
|
||||
|
||||
context "on approval" do
|
||||
setup do
|
||||
@script = %q(
|
||||
|
||||
@@ -20,6 +20,16 @@ module Moderator
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
|
||||
context "#estimate_update_count" do
|
||||
setup do
|
||||
@change = TagBatchChange.new("aaa", "bbb", @user.id, "127.0.0.1")
|
||||
end
|
||||
|
||||
should "find the correct count" do
|
||||
assert_equal(1, @change.estimate_update_count)
|
||||
end
|
||||
end
|
||||
|
||||
should "execute" do
|
||||
tag_batch_change = TagBatchChange.new("aaa", "bbb", @user.id, "127.0.0.1")
|
||||
tag_batch_change.perform
|
||||
|
||||
@@ -58,6 +58,17 @@ class TagAliasTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
context "#estimate_update_count" do
|
||||
setup do
|
||||
FactoryBot.create(:post, tag_string: "aaa bbb ccc")
|
||||
@alias = FactoryBot.create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", status: "pending")
|
||||
end
|
||||
|
||||
should "get the right count" do
|
||||
assert_equal(1, @alias.estimate_update_count)
|
||||
end
|
||||
end
|
||||
|
||||
context "on secondary validation" do
|
||||
should "warn about missing wiki pages" do
|
||||
ti = FactoryBot.build(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", skip_secondary_validations: false)
|
||||
|
||||
@@ -54,6 +54,17 @@ class TagImplicationTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
context "#estimate_update_count" do
|
||||
setup do
|
||||
FactoryBot.create(:post, tag_string: "aaa bbb ccc")
|
||||
@implication = FactoryBot.create(:tag_implication, antecedent_name: "aaa", consequent_name: "bbb", status: "pending")
|
||||
end
|
||||
|
||||
should "get the right count" do
|
||||
assert_equal(1, @implication.estimate_update_count)
|
||||
end
|
||||
end
|
||||
|
||||
context "on secondary validation" do
|
||||
should "warn if either tag is missing a wiki" do
|
||||
ti = FactoryBot.build(:tag_implication, antecedent_name: "aaa", consequent_name: "bbb", skip_secondary_validations: false)
|
||||
|
||||
Reference in New Issue
Block a user