implications: ensure antecedent/consequent tags exist.
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
class TagAlias < TagRelationship
|
class TagAlias < TagRelationship
|
||||||
before_save :ensure_tags_exist
|
|
||||||
after_save :clear_all_cache
|
after_save :clear_all_cache
|
||||||
after_destroy :clear_all_cache
|
after_destroy :clear_all_cache
|
||||||
after_save :clear_all_cache, if: ->(rec) {rec.is_retired?}
|
after_save :clear_all_cache, if: ->(rec) {rec.is_retired?}
|
||||||
@@ -145,11 +144,6 @@ class TagAlias < TagRelationship
|
|||||||
end
|
end
|
||||||
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
|
def ensure_category_consistency
|
||||||
if antecedent_tag.category != consequent_tag.category && antecedent_tag.category != Tag.categories.general
|
if antecedent_tag.category != consequent_tag.category && antecedent_tag.category != Tag.categories.general
|
||||||
consequent_tag.update_attribute(:category, antecedent_tag.category)
|
consequent_tag.update_attribute(:category, antecedent_tag.category)
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ class TagRelationship < ApplicationRecord
|
|||||||
belongs_to :approver, class_name: "User", optional: true
|
belongs_to :approver, class_name: "User", optional: true
|
||||||
belongs_to :forum_post, optional: true
|
belongs_to :forum_post, optional: true
|
||||||
belongs_to :forum_topic, optional: true
|
belongs_to :forum_topic, optional: true
|
||||||
has_one :antecedent_tag, :class_name => "Tag", :foreign_key => "name", :primary_key => "antecedent_name"
|
belongs_to :antecedent_tag, class_name: "Tag", foreign_key: "antecedent_name", primary_key: "name", default: -> { Tag.find_or_create_by_name(antecedent_name) }
|
||||||
has_one :consequent_tag, :class_name => "Tag", :foreign_key => "name", :primary_key => "consequent_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 :antecedent_wiki, through: :antecedent_tag, source: :wiki_page
|
||||||
has_one :consequent_wiki, through: :consequent_tag, source: :wiki_page
|
has_one :consequent_wiki, through: :consequent_tag, source: :wiki_page
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,13 @@ class TagImplicationTest < ActiveSupport::TestCase
|
|||||||
assert_equal(CurrentUser.user.id, ti.creator_id)
|
assert_equal(CurrentUser.user.id, ti.creator_id)
|
||||||
end
|
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
|
should "not validate when a tag directly implicates itself" do
|
||||||
ti = FactoryBot.build(:tag_implication, antecedent_name: "a", consequent_name: "a")
|
ti = FactoryBot.build(:tag_implication, antecedent_name: "a", consequent_name: "a")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user