From b37fa87d4bb0b9b61e2c0abc7781eeca3bd2c93f Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 27 Aug 2020 22:39:16 -0500 Subject: [PATCH] posts: fix bug preventing *_(cosplay) tags from being added. Bug: if `hatsune_miku` is an empty general tag, then tagging a post with `hatsune_miku_(cosplay)` would fail because `hatsune_miku` wasn't a character tag. This could cause tagging posts to fail when aliasing a character tag back to an old name that was used in the past, but was now an empty general tag. Fix: only treat the *_(cosplay) tag as being in conflict with the base tag when the base tag is nonempty. --- app/logical/tag_name_validator.rb | 2 +- test/unit/post_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/logical/tag_name_validator.rb b/app/logical/tag_name_validator.rb index 85c714fde..b4886f33d 100644 --- a/app/logical/tag_name_validator.rb +++ b/app/logical/tag_name_validator.rb @@ -31,7 +31,7 @@ class TagNameValidator < ActiveModel::EachValidator tag_name = TagAlias.to_aliased([$1]).first tag = Tag.find_by_name(tag_name) - if tag.present? && !tag.character? + if tag.present? && !tag.empty? && !tag.character? record.errors[attribute] << "#{tag_name} must be a character tag" end end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index c45d83bc3..ef5c02c21 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -1296,6 +1296,14 @@ class PostTest < ActiveSupport::TestCase refute(@post.has_tag?("cosplay")) assert(@post.warnings[:base].grep(/Couldn't add tag/).present?) end + + should "allow creating a _(cosplay) tag for an empty general tag" do + @tag = create(:tag, name: "hatsune_miku", post_count: 0, category: Tag.categories.general) + @post = create(:post, tag_string: "hatsune_miku_(cosplay)") + + assert_equal("cosplay hatsune_miku hatsune_miku_(cosplay)", @post.reload.tag_string) + assert_equal(true, @tag.reload.character?) + end end context "that has been updated" do