From a56c1f3350c62f742e8360b1bb1c135bcd186fe6 Mon Sep 17 00:00:00 2001 From: Toks Date: Sat, 7 Jun 2014 19:45:54 -0400 Subject: [PATCH] fix infinite loop --- app/models/tag_alias.rb | 24 +++++++++++++++++++++--- app/models/tag_implication.rb | 9 +++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index 84f7822c5..f7e9473cd 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -7,6 +7,7 @@ class TagAlias < ActiveRecord::Base validates_presence_of :creator_id, :antecedent_name, :consequent_name validates_uniqueness_of :antecedent_name validate :absence_of_transitive_relation + validate :antecedent_and_consequent_are_different belongs_to :creator, :class_name => "User" belongs_to :forum_topic attr_accessible :antecedent_name, :consequent_name, :forum_topic_id, :status @@ -117,23 +118,40 @@ class TagAlias < ActiveRecord::Base end end + def antecedent_and_consequent_are_different + normalize_names + if antecedent_name == consequent_name + self.errors[:base] << "Cannot alias a tag to itself" + false + end + end + def move_aliases_and_implications aliases = TagAlias.where(["consequent_name = ?", antecedent_name]) aliases.each do |ta| ta.consequent_name = self.consequent_name - ta.save + success = ta.save + if !success && ta.errors.full_messages.join("; ") =~ /Cannot alias a tag to itself/ + ta.destroy + end end implications = TagImplication.where(["antecedent_name = ?", antecedent_name]) implications.each do |ti| ti.antecedent_name = self.consequent_name - ti.save + success = ti.save + if !success && ti.errors.full_messages.join("; ") =~ /Cannot implicate a tag to itself/ + ti.destroy + end end implications = TagImplication.where(["consequent_name = ?", antecedent_name]) implications.each do |ti| ti.consequent_name = self.consequent_name - ti.save + success = ti.save + if !success && ti.errors.full_messages.join("; ") =~ /Cannot implicate a tag to itself/ + ti.destroy + end end end diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index 9e74132bd..80f4c94a7 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -10,6 +10,7 @@ class TagImplication < ActiveRecord::Base validate :absence_of_circular_relation validate :antecedent_is_not_aliased validate :consequent_is_not_aliased + validate :antecedent_and_consequent_are_different attr_accessible :antecedent_name, :consequent_name, :descendant_names, :forum_topic_id, :status, :forum_topic module DescendantMethods @@ -144,6 +145,14 @@ class TagImplication < ActiveRecord::Base end end + def antecedent_and_consequent_are_different + normalize_names + if antecedent_name == consequent_name + self.errors[:base] << "Cannot implicate a tag to itself" + false + end + end + def update_posts Post.without_timeout do Post.raw_tag_match(antecedent_name).find_each do |post|