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

This commit is contained in:
Neil Lalonde 2020-08-19 12:17:33 -04:00
parent 96511e4548
commit 298ed5d021
No known key found for this signature in database
GPG Key ID: FF871CA9037D0A91
2 changed files with 10 additions and 1 deletions

View File

@ -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

View File

@ -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