implications: ensure antecedent/consequent tags exist.

This commit is contained in:
evazion 2018-12-26 17:30:07 -06:00
parent c27ba02b88
commit 51b08d2243
3 changed files with 9 additions and 8 deletions

View File

@ -1,5 +1,4 @@
class TagAlias < TagRelationship
before_save :ensure_tags_exist
after_save :clear_all_cache
after_destroy :clear_all_cache
after_save :clear_all_cache, if: ->(rec) {rec.is_retired?}
@ -145,11 +144,6 @@ class TagAlias < TagRelationship
end
end
def ensure_tags_exist
Tag.find_or_create_by_name(antecedent_name)
Tag.find_or_create_by_name(consequent_name)
end
def ensure_category_consistency
if antecedent_tag.category != consequent_tag.category && antecedent_tag.category != Tag.categories.general
consequent_tag.update_attribute(:category, antecedent_tag.category)

View File

@ -10,8 +10,8 @@ class TagRelationship < ApplicationRecord
belongs_to :approver, class_name: "User", optional: true
belongs_to :forum_post, optional: true
belongs_to :forum_topic, optional: true
has_one :antecedent_tag, :class_name => "Tag", :foreign_key => "name", :primary_key => "antecedent_name"
has_one :consequent_tag, :class_name => "Tag", :foreign_key => "name", :primary_key => "consequent_name"
belongs_to :antecedent_tag, class_name: "Tag", foreign_key: "antecedent_name", primary_key: "name", default: -> { Tag.find_or_create_by_name(antecedent_name) }
belongs_to :consequent_tag, class_name: "Tag", foreign_key: "consequent_name", primary_key: "name", default: -> { Tag.find_or_create_by_name(consequent_name) }
has_one :antecedent_wiki, through: :antecedent_tag, source: :wiki_page
has_one :consequent_wiki, through: :consequent_tag, source: :wiki_page

View File

@ -64,6 +64,13 @@ class TagImplicationTest < ActiveSupport::TestCase
assert_equal(CurrentUser.user.id, ti.creator_id)
end
should "ensure both tags exist" do
FactoryBot.create(:tag_implication, antecedent_name: "a", consequent_name: "b")
assert(Tag.exists?(name: "a"))
assert(Tag.exists?(name: "b"))
end
should "not validate when a tag directly implicates itself" do
ti = FactoryBot.build(:tag_implication, antecedent_name: "a", consequent_name: "a")