diff --git a/app/controllers/tag_implications_controller.rb b/app/controllers/tag_implications_controller.rb index 8900c4b62..d60d9d7b7 100644 --- a/app/controllers/tag_implications_controller.rb +++ b/app/controllers/tag_implications_controller.rb @@ -25,7 +25,12 @@ class TagImplicationsController < ApplicationController def destroy @tag_implication = TagImplication.find(params[:id]) @tag_implication.destroy - respond_with(@tag_implication) + respond_with(@tag_implication) do |format| + format.html do + flash[:notice] = "Tag implication was deleted" + redirect_to(tag_implications_path) + end + end end def approve diff --git a/app/logical/upload_error_checker.rb b/app/logical/upload_error_checker.rb index 9ee480560..f9c32afb1 100644 --- a/app/logical/upload_error_checker.rb +++ b/app/logical/upload_error_checker.rb @@ -4,7 +4,7 @@ require 'mail' class UploadErrorChecker def check! - uploads = Upload.where("status like 'error%' and created_at >= ?", 1.hour.ago).all + uploads = Upload.where("status like 'error%' and status not like 'error: RuntimeError - duplicate%' and created_at >= ?", 1.hour.ago).all if uploads.size > 5 mail = Mail.new do from "webmaster@danbooru.donmai.us" diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index 726c3dc74..b0a9ccaac 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -1,6 +1,8 @@ class TagImplication < ActiveRecord::Base before_save :update_descendant_names after_save :update_descendant_names_for_parent + after_destroy :update_descendant_names_for_parent + after_destroy :update_posts_for_destroy belongs_to :creator, :class_name => "User" before_validation :initialize_creator, :on => :create before_validation :normalize_names @@ -90,13 +92,41 @@ class TagImplication < ActiveRecord::Base q = q.where("antecedent_name = ?", params[:antecedent_name]) end + if params[:consequent_name].present? + q = q.where("consequent_name = ?", params[:consequent_name]) + end + q end end + module DeletionMethods + extend ActiveSupport::Concern + + module ClassMethods + def update_posts_for_destroy(creator_id, creator_ip_addr, tag_name) + Post.tag_match("#{tag_name} status:any").find_each do |post| + escaped_tag_name = Regexp.escape(tag_name) + fixed_tags = post.tag_string.sub(/(?:\A| )#{escaped_tag_name}(?:\Z| )/, " ").strip + CurrentUser.scoped(User.find(creator_id), creator_ip_addr) do + post.disable_versioning = true + post.update_attributes( + :tag_string => fixed_tags + ) + end + end + end + end + + def update_posts_for_destroy + TagImplication.delay(:queue => "default").update_posts_for_destroy(CurrentUser.user.id, CurrentUser.ip_addr, consequent_name) + end + end + include DescendantMethods include ParentMethods extend SearchMethods + include DeletionMethods def initialize_creator self.creator_id = CurrentUser.user.id @@ -105,7 +135,7 @@ class TagImplication < ActiveRecord::Base def process! update_column(:status, "processing") - update_posts + update_posts_for_create update_column(:status, "active") update_descendant_names_for_parent rescue Exception => e @@ -120,7 +150,7 @@ class TagImplication < ActiveRecord::Base end end - def update_posts + def update_posts_for_create Post.tag_match("#{antecedent_name} status:any").find_each do |post| escaped_antecedent_name = Regexp.escape(antecedent_name) fixed_tags = post.tag_string.sub(/(?:\A| )#{escaped_antecedent_name}(?:\Z| )/, " #{antecedent_name} #{descendant_names} ").strip diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index e8ac6670b..f1418677b 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -8,6 +8,7 @@ class TagImplicationTest < ActiveSupport::TestCase CurrentUser.ip_addr = "127.0.0.1" @user = FactoryGirl.create(:user) MEMCACHE.flush_all + Delayed::Worker.delay_jobs = false end teardown do @@ -67,7 +68,18 @@ class TagImplicationTest < ActiveSupport::TestCase assert_equal("ddd", ti2.descendant_names) end - should "update the decendants for its parent on create" do + should "update the descendants for its parent on destroy" do + ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb") + ti2 = FactoryGirl.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc") + ti3 = FactoryGirl.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "ddd") + ti2.destroy + ti1.reload + ti3.reload + assert_equal("bbb", ti1.descendant_names) + assert_equal("ddd", ti3.descendant_names) + end + + should "update the descendants for its parent on create" do ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb") ti1.reload assert_equal("active", ti1.status) @@ -97,7 +109,17 @@ class TagImplicationTest < ActiveSupport::TestCase assert_equal("ccc ddd eee", ti2.descendant_names) assert_equal("ddd", ti3.descendant_names) assert_equal("eee", ti4.descendant_names) + end + should "update any affected post upon destroy" do + ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb") + ti2 = FactoryGirl.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc") + ti3 = FactoryGirl.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "ddd") + p1 = FactoryGirl.create(:post, :tag_string => "aaa") + assert_equal("aaa bbb ccc ddd", p1.tag_string) + ti2.destroy + p1.reload + assert_equal("aaa bbb ddd", p1.tag_string) end should "update any affected post upon save" do