tags: add general?, character?, copyright?, artist?, meta?, empty? helper methods.

This commit is contained in:
evazion
2020-05-10 23:28:07 -05:00
parent 49383d393a
commit e3187e0bd0
11 changed files with 37 additions and 34 deletions

View File

@@ -1566,10 +1566,10 @@ class Post < ApplicationRecord
end
def added_tags_are_valid
new_tags = added_tags.select { |t| t.post_count <= 0 }
new_general_tags = new_tags.select { |t| t.category == Tag.categories.general }
new_artist_tags = new_tags.select { |t| t.category == Tag.categories.artist }
repopulated_tags = new_tags.select { |t| (t.category != Tag.categories.general) && (t.category != Tag.categories.meta) && (t.created_at < 1.hour.ago) }
new_tags = added_tags.select(&:empty?)
new_general_tags = new_tags.select(&:general?)
new_artist_tags = new_tags.select(&:artist?)
repopulated_tags = new_tags.select { |t| !t.general? && !t.meta? && (t.created_at < 1.hour.ago) }
if new_general_tags.present?
n = new_general_tags.size
@@ -1604,7 +1604,7 @@ class Post < ApplicationRecord
return if !new_record?
return if source !~ %r!\Ahttps?://!
return if has_tag?("artist_request") || has_tag?("official_art")
return if tags.any? { |t| t.category == Tag.categories.artist }
return if tags.any?(&:artist?)
return if Sources::Strategies.find(source).is_a?(Sources::Strategies::Null)
self.warnings[:base] << "Artist tag is required. \"Create new artist tag\":[/artists/new?artist%5Bsource%5D=#{CGI.escape(source)}]. Ask on the forum if you need naming help"
@@ -1612,7 +1612,7 @@ class Post < ApplicationRecord
def has_copyright_tag
return if !new_record?
return if has_tag?("copyright_request") || tags.any? { |t| t.category == Tag.categories.copyright }
return if has_tag?("copyright_request") || tags.any?(&:copyright?)
self.warnings[:base] << "Copyright tag is required. Consider adding [[copyright request]] or [[original]]"
end
@@ -1620,7 +1620,7 @@ class Post < ApplicationRecord
def has_enough_tags
return if !new_record?
if tags.count { |t| t.category == Tag.categories.general } < 10
if tags.count(&:general?) < 10
self.warnings[:base] << "Uploads must have at least 10 general tags. Read [[howto:tag]] for guidelines on tagging your uploads"
end
end

View File

@@ -52,10 +52,8 @@ class Tag < ApplicationRecord
end
end
module CountMethods
extend ActiveSupport::Concern
module ClassMethods
concerning :CountMethods do
class_methods do
# Lock the tags first in alphabetical order to avoid deadlocks under concurrent updates.
#
# https://stackoverflow.com/questions/44660368/postgres-update-with-order-by-how-to-do-it
@@ -110,10 +108,14 @@ class Tag < ApplicationRecord
tags
end
end
def empty?
post_count <= 0
end
end
module CategoryMethods
module ClassMethods
concerning :CategoryMethods do
class_methods do
def categories
@categories ||= CategoryMapping.new
end
@@ -148,8 +150,11 @@ class Tag < ApplicationRecord
end
end
def self.included(m)
m.extend(ClassMethods)
# define artist?, general?, character?, copyright?, meta?
TagCategory.categories.each do |category_name|
define_method("#{category_name}?") do
category == Tag.categories.send(category_name)
end
end
def category_name
@@ -357,7 +362,5 @@ class Tag < ApplicationRecord
end
include ApiMethods
include CountMethods
include CategoryMethods
extend SearchMethods
end

View File

@@ -91,9 +91,9 @@ class TagAlias < TagRelationship
end
def ensure_category_consistency
if antecedent_tag.category == Tag.categories.general && consequent_tag.category != Tag.categories.general
if antecedent_tag.general? && !consequent_tag.general?
antecedent_tag.update!(category: consequent_tag.category)
elsif consequent_tag.category == Tag.categories.general && antecedent_tag.category != Tag.categories.general
elsif consequent_tag.general? && !antecedent_tag.general?
consequent_tag.update!(category: antecedent_tag.category)
end
end
@@ -106,7 +106,7 @@ class TagAlias < TagRelationship
end
end
if antecedent_tag.category == Tag.categories.artist
if antecedent_tag.artist?
if antecedent_tag.artist.present? && consequent_tag.artist.blank?
antecedent_tag.artist.update!(name: consequent_name)
end

View File

@@ -119,7 +119,7 @@ class WikiPage < ApplicationRecord
return unless title_changed?
tag_was = Tag.find_by_name(Tag.normalize_name(title_was))
if tag_was.present? && tag_was.post_count > 0
if tag_was.present? && !tag_was.empty?
warnings[:base] << %!Warning: {{#{title_was}}} still has #{tag_was.post_count} #{"post".pluralize(tag_was.post_count)}. Be sure to move the posts!
end