Skip to content

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

FIX: delete unused tags shouldn't delete tags belonging to tag groups

Neil Lalonde298ed5d0211a
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