From 298ed5d0211a0f98ec452b06385b13d36f1d643c Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 19 Aug 2020 12:17:33 -0400 Subject: [PATCH] FIX: delete unused tags shouldn't delete tags belonging to tag groups --- app/models/tag.rb | 8 +++++++- spec/models/tag_spec.rb | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index 9f6ade238e0..b9a510175da 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -21,7 +21,13 @@ class Tag < ActiveRecord::Base where("lower(tags.name) IN (?)", name) end - scope :unused, -> { where(topic_count: 0, pm_topic_count: 0) } + # tags that have never been used and don't belong to a tag group + scope :unused, -> do + where(topic_count: 0, pm_topic_count: 0) + .joins("LEFT JOIN tag_group_memberships tgm ON tags.id = tgm.tag_id") + .where("tgm.tag_id IS NULL") + end + scope :base_tags, -> { where(target_tag_id: nil) } has_many :tag_users, dependent: :destroy # notification settings diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 8bf0f7e7fe2..e31b15b8072 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -194,6 +194,9 @@ describe Tag do Fabricate(:tag, name: "unused2", topic_count: 0, pm_topic_count: 0)] end + let(:tag_in_group) { Fabricate(:tag, name: "unused_in_group", topic_count: 0, pm_topic_count: 0) } + let!(:tag_group) { Fabricate(:tag_group, tag_names: [tag_in_group.name]) } + it "returns the correct tags" do expect(Tag.unused.pluck(:name)).to contain_exactly("unused1", "unused2") end