implications: refactor automatic tags.

Move #automatic_tags_for out of TagImplication since it doesn't really
belong here.
This commit is contained in:
evazion
2020-12-02 12:49:14 -06:00
parent 6485a053a5
commit 86e4c21e48
4 changed files with 12 additions and 22 deletions

View File

@@ -478,7 +478,7 @@ class Post < ApplicationRecord
normalized_tags = add_automatic_tags(normalized_tags)
normalized_tags = remove_invalid_tags(normalized_tags)
normalized_tags = Tag.convert_cosplay_tags(normalized_tags)
normalized_tags += Tag.create_for_list(TagImplication.automatic_tags_for(normalized_tags))
normalized_tags += Tag.create_for_list(Tag.automatic_tags_for(normalized_tags))
normalized_tags += TagImplication.tags_implied_by(normalized_tags).map(&:name)
normalized_tags = normalized_tags.compact.uniq.sort
normalized_tags = Tag.create_for_list(normalized_tags)

View File

@@ -335,6 +335,14 @@ class Tag < ApplicationRecord
end
end
def self.automatic_tags_for(names)
tags = []
tags += names.grep(/\A(.+)_\(cosplay\)\z/i) { "char:#{TagAlias.to_aliased([$1]).first}" }
tags << "cosplay" if names.any?(/_\(cosplay\)\z/i)
tags << "school_uniform" if names.any?(/_school_uniform\z/i)
tags.uniq
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

View File

@@ -8,20 +8,6 @@ class TagImplication < TagRelationship
validate :antecedent_is_not_aliased
validate :consequent_is_not_aliased
module DescendantMethods
extend ActiveSupport::Concern
module ClassMethods
def automatic_tags_for(names)
tags = []
tags += names.grep(/\A(.+)_\(cosplay\)\z/i) { "char:#{TagAlias.to_aliased([$1]).first}" }
tags << "cosplay" if names.any?(/_\(cosplay\)\z/i)
tags << "school_uniform" if names.any?(/_school_uniform\z/i)
tags.uniq
end
end
end
concerning :HierarchyMethods do
class_methods do
def ancestors_of(names)
@@ -64,7 +50,7 @@ class TagImplication < TagRelationship
end
end
module ValidationMethods
concerning :ValidationMethods do
def absence_of_circular_relation
return if is_rejected?
@@ -107,7 +93,7 @@ class TagImplication < TagRelationship
end
end
module ApprovalMethods
concerning :ApprovalMethods do
def process!
update_posts!
end
@@ -121,8 +107,4 @@ class TagImplication < TagRelationship
end
end
end
include DescendantMethods
include ValidationMethods
include ApprovalMethods
end

View File

@@ -14,7 +14,7 @@
</p>
<% end %>
<% TagImplication.automatic_tags_for([tag.name]).tap do |automatic_tags| %>
<% Tag.automatic_tags_for([tag.name]).tap do |automatic_tags| %>
<% if automatic_tags.present? %>
<p class="fineprint">
This tag automatically adds