A platform for community discussion. Free, open, simple.
FIX: Recursively tag topics with missing ancestor tags (#18344)
Selase Krakani049f8569d892
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rbindex 8e64bab2a56..e3832fd0e12 100644--- a/lib/discourse_tagging.rb+++ b/lib/discourse_tagging.rb@@ -13,6 +13,29 @@ module DiscourseTaggingON tgm.tag_group_id = tg.idSQL+ ANCESTOR_TAG_IDS_SQL ||= <<~SQL+ WITH RECURSIVE ancestors AS (+ SELECT+ tgm.tag_id,+ tg.parent_tag_id+ FROM+ tag_group_memberships tgm+ INNER JOIN tag_groups tg ON tg.id = tgm.tag_group_id+ WHERE+ tg.parent_tag_id IS NOT NULL+ AND tgm.tag_id IN (:tag_ids)+ UNION+ SELECT+ tgm.tag_id,+ tg.parent_tag_id+ FROM+ tag_group_memberships tgm+ INNER JOIN tag_groups tg ON tg.id = tgm.tag_group_id+ INNER JOIN ancestors ON tgm.tag_id = ancestors.parent_tag_id+ )+ SELECT * FROM ancestors+ SQL+def self.tag_topic_by_names(topic, guardian, tag_names_arg, append: false)if guardian.can_tag?(topic)tag_names = DiscourseTagging.tags_for_saving(tag_names_arg, guardian) || []@@ -93,14 +116,7 @@ module DiscourseTagging# add missing mandatory parent tagstag_ids = tags.map(&:id)- parent_tags_map = DB.query("- SELECT tgm.tag_id, tg.parent_tag_id- FROM tag_groups tg- INNER JOIN tag_group_memberships tgm- ON tgm.tag_group_id = tg.id- WHERE tg.parent_tag_id IS NOT NULL- AND tgm.tag_id IN (?)- ", tag_ids).inject({}) do |h, v|+ parent_tags_map = DB.query(ANCESTOR_TAG_IDS_SQL, tag_ids: tag_ids).inject({}) do |h, v|h[v.tag_id] ||= []h[v.tag_id] << v.parent_tag_idhdiff --git a/spec/lib/discourse_tagging_spec.rb b/spec/lib/discourse_tagging_spec.rbindex ddcd88916d7..548489c31b7 100644--- a/spec/lib/discourse_tagging_spec.rb+++ b/spec/lib/discourse_tagging_spec.rb@@ -497,9 +497,19 @@ RSpec.describe DiscourseTagging doendit "adds all parent tags that are missing" do+ parent_tag_group = Fabricate(:tag_group)parent_tag = Fabricate(:tag, name: 'parent')+ parent_tag_group.tags = [parent_tag]+tag_group2 = Fabricate(:tag_group, parent_tag_id: parent_tag.id)tag_group2.tags = [tag2]++ tag_group1 = Fabricate(:tag_group)+ tag_group1.tags = [tag1]++ tag_group3 = Fabricate(:tag_group, parent_tag_id: tag1.id)+ tag_group3.tags = [tag3]+valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(user), [tag3.name, tag2.name])expect(valid).to eq(true)expect(topic.reload.tags.map(&:name)).to contain_exactly(@@ -519,6 +529,49 @@ RSpec.describe DiscourseTagging doexpect(valid).to eq(true)expect(topic.reload.tags.map(&:name)).to contain_exactly(*[parent_tag, common].map(&:name))end++ context "when tag group has grandparents" do+ let(:tag_group1) { Fabricate(:tag_group) }+ let(:foo) { Fabricate(:tag, name: 'foo') }+ let(:tag_group2) { Fabricate(:tag_group, parent_tag_id: foo.id) }+ let(:bar) { Fabricate(:tag, name: 'bar') }+ let(:tag_group3) { Fabricate(:tag_group, parent_tag_id: bar.id) }+ let(:baz) { Fabricate(:tag, name: 'baz') }++ before do+ tag_group1.tags = [foo]+ tag_group2.tags = [bar]+ tag_group3.tags = [baz]+ end++ it "recursively adds all ancestors" do+ valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(user), [baz.name])+ expect(valid).to eq(true)+ expect(topic.reload.tags.map(&:name)).to contain_exactly(*[foo, bar, baz].map(&:name))+ end++ it "adds only one parent when multiple parents exist" do+ alt_parent_group = Fabricate(:tag_group)+ alt = Fabricate(:tag, name: 'alt')+ alt_parent_group.tags = [alt]++ alt_baz_group = Fabricate(:tag_group, parent_tag_id: alt.id)+ alt_baz_group.tags = [baz]++ valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(user), [baz.name])+ expect(valid).to eq(true)+ expect(topic.reload.tags.map(&:name)).to contain_exactly(*[foo, bar, baz].map(&:name))+ end++ it "adds missing tags even with cycles" do+ tag_group4 = Fabricate(:tag_group, parent_tag_id: baz.id)+ tag_group4.tags = [foo]++ valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(user), [baz.name])+ expect(valid).to eq(true)+ expect(topic.reload.tags.map(&:name)).to contain_exactly(*[foo, bar, baz].map(&:name))+ end+ endendcontext "when enforcing required tags from a tag group" do