FIX: 500 error when adding restricted category tags (#21147)

This fixes a 500 error that occurs when adding a tag to a category's
restricted tag list if the category's restricted tags already included a
synonym tag.
This commit is contained in:
Blake Erickson 2023-04-18 11:01:11 -06:00 committed by GitHub
parent 26543a5b59
commit 76874b7098
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -634,7 +634,10 @@ module DiscourseTagging
taggable.tags = Tag.where_name(tag_names).all taggable.tags = Tag.where_name(tag_names).all
new_tag_names = new_tag_names =
taggable.tags.size < tag_names.size ? tag_names - taggable.tags.map(&:name) : [] taggable.tags.size < tag_names.size ? tag_names - taggable.tags.map(&:name) : []
taggable.tags << Tag.where(target_tag_id: taggable.tags.map(&:id)).all taggable.tags << Tag
.where(target_tag_id: taggable.tags.map(&:id))
.where.not(id: taggable.tags.map(&:id))
.all
new_tag_names.each { |name| taggable.tags << Tag.create(name: name) } new_tag_names.each { |name| taggable.tags << Tag.create(name: name) }
end end
end end

View File

@ -1425,4 +1425,28 @@ RSpec.describe Category do
) )
end end
end end
describe "allowed_tags=" do
let(:category) { Fabricate(:category) }
fab!(:tag) { Fabricate(:tag) }
fab!(:tag2) { Fabricate(:tag) }
before { SiteSetting.tagging_enabled = true }
it "can use existing tags for category tags" do
category.allowed_tags = [tag.name]
expect_same_tag_names(category.reload.tags, [tag])
end
context "with synonyms" do
fab!(:synonym) { Fabricate(:tag, name: "synonym", target_tag: tag) }
it "can use existing tags for category tags" do
category.allowed_tags = [tag.name, synonym.name]
category.reload
category.allowed_tags = [tag.name, synonym.name, tag2.name]
expect_same_tag_names(category.reload.tags, [tag.name, synonym.name, tag2.name])
end
end
end
end end