From f626c7ebd772c116b09227e4f30090319d0e83c3 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Mon, 15 Jan 2018 18:00:47 -0800 Subject: [PATCH] Fix how cosplay tags are handled --- app/models/post.rb | 10 ++++++++++ app/models/tag.rb | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index 7507b0efd..3cddf75cb 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -714,6 +714,8 @@ class Post < ApplicationRecord normalized_tags = TagAlias.to_aliased(normalized_tags) normalized_tags = %w(tagme) if normalized_tags.empty? normalized_tags = add_automatic_tags(normalized_tags) + normalized_tags = remove_invalid_tags(normalized_tags) + normalized_tags = Tag.convert_cosplay_tags(normalized_tags) normalized_tags = normalized_tags + Tag.create_for_list(TagImplication.automatic_tags_for(normalized_tags)) normalized_tags = TagImplication.with_descendants(normalized_tags) normalized_tags = normalized_tags.compact.uniq.sort @@ -721,6 +723,14 @@ class Post < ApplicationRecord set_tag_string(normalized_tags.join(" ")) end + def remove_invalid_tags(tags) + invalid_tags = Tag.invalid_cosplay_tags(tags) + if invalid_tags.present? + self.warnings[:base] << "The root tag must be a character tag: #{invalid_tags.map {|tag| "[b]#{tag}[/b]" }.join(", ")}" + end + tags - invalid_tags + end + def remove_negated_tags(tags) @negated_tags, tags = tags.partition {|x| x =~ /\A-/i} @negated_tags = @negated_tags.map {|x| x[1..-1]} diff --git a/app/models/tag.rb b/app/models/tag.rb index 918caf46b..2556ee5e3 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -950,6 +950,19 @@ class Tag < ApplicationRecord end end + def self.convert_cosplay_tags(tags) + cosplay_tags,other_tags = tags.partition {|tag| tag.match(/\A(.+)_\(cosplay\)\Z/) } + cosplay_tags.grep(/\A(.+)_\(cosplay\)\Z/) { "#{TagAlias.to_aliased([$1]).first}_(cosplay)" } + other_tags + end + + def self.invalid_cosplay_tags(tags) + tags.grep(/\A(.+)_\(cosplay\)\Z/) {|match| [match,TagAlias.to_aliased([$1]).first] }. + select do |name| + tag = Tag.find_by_name(name[1]) + !tag.nil? && tag.category != Tag.categories.character + end.map {|tag| tag[0]} + end + def editable_by?(user) return true if user.is_admin? return true if !is_locked? && user.is_builder? && post_count < 1_000