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

@@ -14,9 +14,9 @@ class ModqueueController < ApplicationController
@uploaders = @modqueue_posts.group(:uploader).order(count: :desc).limit(20).count
@tags = RelatedTagCalculator.frequent_tags_for_post_relation(@modqueue_posts)
@artist_tags = @tags.select { |tag| tag.category == Tag.categories.artist }.sort_by(&:overlap_count).reverse.take(10)
@copyright_tags = @tags.select { |tag| tag.category == Tag.categories.copyright }.sort_by(&:overlap_count).reverse.take(10)
@character_tags = @tags.select { |tag| tag.category == Tag.categories.character }.sort_by(&:overlap_count).reverse.take(10)
@artist_tags = @tags.select(&:artist?).sort_by(&:overlap_count).reverse.take(10)
@copyright_tags = @tags.select(&:copyright?).sort_by(&:overlap_count).reverse.take(10)
@character_tags = @tags.select(&:character?).sort_by(&:overlap_count).reverse.take(10)
respond_with(@posts)
end

View File

@@ -37,7 +37,7 @@ class DText
tag = tags.find { |tag| tag.name == name }
artist = artists.find { |artist| artist.name == name }
if tag.present? && tag.category == Tag.categories.artist
if tag.present? && tag.artist?
node["href"] = "/artists/show_or_new?name=#{CGI.escape(name)}"
if artist.blank?
@@ -45,7 +45,7 @@ class DText
node["title"] = "This artist page does not exist"
end
node["class"] += " tag-type-#{Tag.categories.artist}"
node["class"] += " tag-type-#{tag.category}"
else
if wiki.blank?
node["class"] += " dtext-wiki-does-not-exist"
@@ -57,7 +57,7 @@ class DText
elsif tag.blank?
node["class"] += " dtext-tag-does-not-exist"
node["title"] = "This wiki page does not have a tag"
elsif tag.post_count <= 0
elsif tag.empty?
node["class"] += " dtext-tag-empty"
node["title"] = "This wiki page does not have a tag"
else

View File

@@ -34,7 +34,7 @@ module PostSets
end
def artist
return nil unless tag.present? && tag.category == Tag.categories.artist
return nil unless tag.present? && tag.artist?
return nil unless tag.artist.present? && !tag.artist.is_deleted?
tag.artist
end

View File

@@ -213,7 +213,7 @@ module Sources
def translated_tags
translated_tags = normalized_tags.flat_map(&method(:translate_tag)).uniq.sort
translated_tags.reject { |tag| tag.category == Tag.categories.artist }
translated_tags.reject(&:artist?)
end
# Given a tag from the source site, should return an array of corresponding Danbooru tags.

View File

@@ -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.category != Tag.categories.character
if tag.present? && !tag.character?
record.errors[attribute] << "#{tag_name} must be a character tag"
end
end

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

View File

@@ -110,7 +110,7 @@ class TagSetPresenter
html = %{<li class="tag-type-#{tag.category}" data-tag-name="#{h(name)}">}
unless name_only
if category == Tag.categories.artist
if tag.artist?
html << %{<a class="wiki-link" href="/artists/show_or_new?name=#{u(name)}">?</a> }
elsif name =~ /\A\d+\z/
html << %{<a class="wiki-link" href="/wiki_pages/~#{u(name)}">?</a> }
@@ -125,7 +125,7 @@ class TagSetPresenter
end
humanized_tag = humanize_tags ? name.tr("_", " ") : name
itemprop = 'itemprop="author"' if category == Tag.categories.artist
itemprop = 'itemprop="author"' if tag.artist?
html << %{<a class="search-tag" #{itemprop} href="/posts?tags=#{u(name)}">#{h(humanized_tag)}</a> }
unless name_only || tag.new_record?
@@ -137,7 +137,7 @@ class TagSetPresenter
post_count = count
end
is_underused_tag = count <= 1 && category == Tag.categories.general
is_underused_tag = count <= 1 && tag.general?
klass = "post-count#{is_underused_tag ? " low-post-count" : ""}"
html << %{<span class="#{klass}" title="#{count}">#{post_count}</span>}

View File

@@ -1,4 +1,4 @@
<% if wiki_page.tag.present? && wiki_page.tag.post_count > 0 %>
<% if wiki_page.tag.present? && !wiki_page.tag.empty? %>
<div id="wiki-page-posts">
<h2>Recent Posts <%= link_to "»", posts_path(tags: wiki_page.title) %></h2>