diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index b5f973fae..78fe61930 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -5,7 +5,7 @@ class TagAlias < TagRelationship after_save :create_mod_action validates_uniqueness_of :antecedent_name validate :absence_of_transitive_relation - validate :consequent_has_wiki_page, on: :create, unless: :skip_secondary_validations + validate :wiki_pages_present, on: :create, unless: :skip_secondary_validations validate :mininum_antecedent_count, on: :create, unless: :skip_secondary_validations module CacheMethods @@ -195,8 +195,10 @@ class TagAlias < TagRelationship destroy end - def consequent_has_wiki_page - if consequent_wiki.nil? + def wiki_pages_present + if antecedent_wiki.present? && consequent_wiki.present? + errors[:base] << conflict_message + elsif antecedent_wiki.blank? && consequent_wiki.blank? errors[:base] << "The #{consequent_name} tag needs a corresponding wiki page" end end diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index e9eba20ab..4401df417 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -42,6 +42,25 @@ class TagAliasTest < ActiveSupport::TestCase should_not allow_value(nil).for(:creator_id) should_not allow_value(-1).for(:creator_id).with_message("must exist", against: :creator) + + 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) + + assert(ti.invalid?) + assert_includes(ti.errors[:base], "The bbb tag needs a corresponding wiki page") + end + + should "warn about conflicting wiki pages" do + FactoryBot.create(:wiki_page, title: "aaa", body: "aaa") + FactoryBot.create(:wiki_page, title: "bbb", body: "bbb") + ti = FactoryBot.build(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", skip_secondary_validations: false) + + assert(ti.invalid?) + assert_includes(ti.errors[:base], "The tag alias [[aaa]] -> [[bbb]] has conflicting wiki pages. [[bbb]] should be updated to include information from [[aaa]] if necessary.") + end end should "populate the creator information" do