posts: fix exception when tagging post with char:copy:foo.

Fixup for 015c6dc7d. Show a warning about failure to add a tag instead
of raising an exception when trying to tag a post with `char:copy:foo`.
This tries to create a tag named `copy:foo` then set the category to
character, which doesn't work because `copy:foo` isn't a valid tag name.
This commit is contained in:
evazion
2022-09-10 14:36:02 -05:00
parent 015c6dc7db
commit 22bfa44183
2 changed files with 18 additions and 18 deletions

View File

@@ -197,7 +197,7 @@ class Tag < ApplicationRecord
tag = find_or_create_by(name: normalize_name(name)) tag = find_or_create_by(name: normalize_name(name))
if category.present? && current_user.present? && Pundit.policy!(current_user, tag).can_change_category? if category.present? && current_user.present? && Pundit.policy!(current_user, tag).can_change_category?
tag.update!(category: categories.value_for(category)) tag.update(category: categories.value_for(category))
end end
tag tag

View File

@@ -523,28 +523,28 @@ class PostTest < ActiveSupport::TestCase
end end
context "tagged with a metatag" do context "tagged with a metatag" do
context "for typing a tag" do context "for a tag category prefix" do
setup do should "set the category of a new tag" do
@post = FactoryBot.create(:post, tag_string: "char:hoge") create(:post, tag_string: "char:hoge")
@tags = @post.tag_array
assert_equal(Tag.categories.character, Tag.find_by_name("hoge").category)
end end
should "change the type" do should "change the category for an aliased tag" do
assert(Tag.where(name: "hoge", category: 4).exists?, "expected 'moge' tag to be created as a character") create(:tag_alias, antecedent_name: "hoge", consequent_name: "moge")
end post = create(:post, tag_string: "char:hoge")
end
context "for typing an aliased tag" do assert_equal(["moge"], post.tag_array)
setup do assert_equal(Tag.categories.general, Tag.find_by_name("moge").category)
@alias = FactoryBot.create(:tag_alias, antecedent_name: "hoge", consequent_name: "moge") assert_equal(Tag.categories.character, Tag.find_by_name("hoge").category)
@post = FactoryBot.create(:post, tag_string: "char:hoge")
@tags = @post.tag_array
end end
should "change the type" do should "not raise an exception for an invalid tag name" do
assert_equal(["moge"], @tags) post = create(:post, tag_string: "tagme char:copy:blah")
assert(Tag.where(name: "moge", category: 0).exists?, "expected 'moge' tag to be created as a character")
assert(Tag.where(name: "hoge", category: 4).exists?, "expected 'moge' tag to be created as a character") assert_match(/Couldn't add tag: 'copy:blah' cannot begin with 'copy:'/, post.warnings[:base].join("\n"))
assert_equal(["tagme"], post.tag_array)
assert_equal(false, Tag.exists?(name: "copy:blah"))
end end
end end