FIX: remove parent tag from tag group

Having a tag be a member of a tag group and the group's parent tag at
the same time causes some unexpected behavior. When a tag is assigned
as the parent, remove it from the group.
This commit is contained in:
Neil Lalonde 2020-03-13 12:25:46 -04:00
parent ad2ec5b65e
commit 7c27f9bba9
No known key found for this signature in database
GPG Key ID: FF871CA9037D0A91
2 changed files with 21 additions and 0 deletions

View File

@ -13,6 +13,7 @@ class TagGroup < ActiveRecord::Base
before_create :init_permissions before_create :init_permissions
before_save :apply_permissions before_save :apply_permissions
before_save :remove_parent_from_group
after_commit { DiscourseTagging.clear_cache! } after_commit { DiscourseTagging.clear_cache! }
@ -73,6 +74,10 @@ class TagGroup < ActiveRecord::Base
end end
end end
def remove_parent_from_group
tags.delete(parent_tag) if tags.include?(parent_tag)
end
def self.visible(guardian) def self.visible(guardian)
if guardian.is_staff? if guardian.is_staff?
TagGroup TagGroup

View File

@ -101,6 +101,22 @@ describe TagGroup do
expect_same_tag_names(tag_group.reload.tags, [tag, 'new-tag']) expect_same_tag_names(tag_group.reload.tags, [tag, 'new-tag'])
end end
it "removes parent tag as group member" do
parent = Fabricate(:tag)
tag_group.tags = [tag, parent]
tag_group.update!(parent_tag: parent)
tag_group.reload
expect_same_tag_names(tag_group.tags, [tag])
expect_same_tag_names([tag_group.parent_tag], [parent])
end
it "removes parent tag as group member when creating the group" do
parent = Fabricate(:tag)
tg = Fabricate(:tag_group, tags: [tag, parent], parent_tag: parent)
expect_same_tag_names(tg.tags, [tag])
expect_same_tag_names([tg.parent_tag], [parent])
end
context 'with synonyms' do context 'with synonyms' do
fab!(:synonym) { Fabricate(:tag, name: 'synonym', target_tag: tag) } fab!(:synonym) { Fabricate(:tag, name: 'synonym', target_tag: tag) }