Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep existent tags when editing tag topics (#15050)

Dan Ungureanu948a1523ea9f
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb
index 96dd3274a44..16e240ade53 100644
--- a/lib/discourse_tagging.rb
+++ b/lib/discourse_tagging.rb
@@ -77,6 +77,9 @@ module DiscourseTagging
only_tag_names: tag_names
)
+ # keep existent tags that current user cannot use
+ tags += Tag.where(name: old_tag_names & tag_names)
+
tags = Tag.where(id: tags.map(&:id)).all.to_a if tags.size > 0
if tags.size < tag_names.size && (category.nil? || category.allow_global_tags || (category.tags.count == 0 && category.tag_groups.count == 0))
diff --git a/spec/components/discourse_tagging_spec.rb b/spec/components/discourse_tagging_spec.rb
index 815e399d263..5ef7c3e7eec 100644
--- a/spec/components/discourse_tagging_spec.rb
+++ b/spec/components/discourse_tagging_spec.rb
@@ -383,6 +383,18 @@ describe DiscourseTagging do
expect(topic.tags.pluck(:name)).to be_blank
end
+ it 'can remove tags and keep existent ones' do
+ tag1 = Fabricate(:tag)
+ tag2 = Fabricate(:tag)
+ topic = Fabricate(:topic, tags: [tag1, tag2])
+ Fabricate(:category, allowed_tags: [tag1.name])
+
+ result = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag1.name])
+
+ expect(result).to eq(true)
+ expect(topic.reload.tags.pluck(:name)).to eq([tag1.name])
+ end
+
context 'respects category minimum_required_tags setting' do
fab!(:category) { Fabricate(:category, minimum_required_tags: 2) }
fab!(:topic) { Fabricate(:topic, category: category) }