From 293b2c0be8fb662c65ed57da9263061da9c292c4 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 1 Dec 2020 16:10:30 -0600 Subject: [PATCH] implications: don't update posts that already have the new tag. When approving an implication, only retag posts that are missing the new tag. Don't try to update posts that already have the tag. This makes large implication requests faster to process when most of the posts already have the implied tag. --- app/models/tag_implication.rb | 9 ++++++++- app/models/tag_relationship.rb | 10 ---------- test/unit/tag_implication_test.rb | 9 +++++---- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index f8dc34bbe..ab7da6bc8 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -110,8 +110,15 @@ class TagImplication < TagRelationship module ApprovalMethods def process! + update_posts! + end + + def update_posts! CurrentUser.scoped(User.system) do - update_posts + Post.system_tag_match("#{antecedent_name} -#{consequent_name}").find_each do |post| + post.lock! + post.save! + end end end diff --git a/app/models/tag_relationship.rb b/app/models/tag_relationship.rb index 3693f84cb..d6b0f5c73 100644 --- a/app/models/tag_relationship.rb +++ b/app/models/tag_relationship.rb @@ -118,16 +118,6 @@ class TagRelationship < ApplicationRecord end end - def update_posts - Post.without_timeout do - Post.raw_tag_match(antecedent_name).find_each do |post| - post.with_lock do - post.save! - end - end - end - end - def self.approve!(antecedent_name:, consequent_name:, approver:, forum_topic: nil) ProcessTagRelationshipJob.perform_later(class_name: self.name, approver: approver, antecedent_name: antecedent_name, consequent_name: consequent_name, forum_topic: forum_topic) end diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index 99a84de10..f0f8d3625 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -116,13 +116,14 @@ class TagImplicationTest < ActiveSupport::TestCase end should "update any affected post upon save" do - p1 = FactoryBot.create(:post, :tag_string => "aaa bbb ccc") + p1 = create(:post, tag_string: "sword") + p2 = create(:post, tag_string: "sword weapon") - TagImplication.approve!(antecedent_name: "aaa", consequent_name: "xxx", approver: @admin) - TagImplication.approve!(antecedent_name: "aaa", consequent_name: "yyy", approver: @admin) + TagImplication.approve!(antecedent_name: "sword", consequent_name: "weapon", approver: @admin) perform_enqueued_jobs - assert_equal("aaa bbb ccc xxx yyy", p1.reload.tag_string) + assert_equal("sword weapon", p1.reload.tag_string) + assert_equal("sword weapon", p2.reload.tag_string) end context "when calculating implied tags" do