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 @uploaders = @modqueue_posts.group(:uploader).order(count: :desc).limit(20).count
@tags = RelatedTagCalculator.frequent_tags_for_post_relation(@modqueue_posts) @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) @artist_tags = @tags.select(&: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) @copyright_tags = @tags.select(&: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) @character_tags = @tags.select(&:character?).sort_by(&:overlap_count).reverse.take(10)
respond_with(@posts) respond_with(@posts)
end end

View File

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

View File

@@ -34,7 +34,7 @@ module PostSets
end end
def artist 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? return nil unless tag.artist.present? && !tag.artist.is_deleted?
tag.artist tag.artist
end end

View File

@@ -213,7 +213,7 @@ module Sources
def translated_tags def translated_tags
translated_tags = normalized_tags.flat_map(&method(:translate_tag)).uniq.sort 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 end
# Given a tag from the source site, should return an array of corresponding Danbooru tags. # 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_name = TagAlias.to_aliased([$1]).first
tag = Tag.find_by_name(tag_name) 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" record.errors[attribute] << "#{tag_name} must be a character tag"
end end
end end

View File

@@ -1566,10 +1566,10 @@ class Post < ApplicationRecord
end end
def added_tags_are_valid def added_tags_are_valid
new_tags = added_tags.select { |t| t.post_count <= 0 } new_tags = added_tags.select(&:empty?)
new_general_tags = new_tags.select { |t| t.category == Tag.categories.general } new_general_tags = new_tags.select(&:general?)
new_artist_tags = new_tags.select { |t| t.category == Tag.categories.artist } new_artist_tags = new_tags.select(&:artist?)
repopulated_tags = new_tags.select { |t| (t.category != Tag.categories.general) && (t.category != Tag.categories.meta) && (t.created_at < 1.hour.ago) } repopulated_tags = new_tags.select { |t| !t.general? && !t.meta? && (t.created_at < 1.hour.ago) }
if new_general_tags.present? if new_general_tags.present?
n = new_general_tags.size n = new_general_tags.size
@@ -1604,7 +1604,7 @@ class Post < ApplicationRecord
return if !new_record? return if !new_record?
return if source !~ %r!\Ahttps?://! return if source !~ %r!\Ahttps?://!
return if has_tag?("artist_request") || has_tag?("official_art") 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) 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" 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 def has_copyright_tag
return if !new_record? 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]]" self.warnings[:base] << "Copyright tag is required. Consider adding [[copyright request]] or [[original]]"
end end
@@ -1620,7 +1620,7 @@ class Post < ApplicationRecord
def has_enough_tags def has_enough_tags
return if !new_record? 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" self.warnings[:base] << "Uploads must have at least 10 general tags. Read [[howto:tag]] for guidelines on tagging your uploads"
end end
end end

View File

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

View File

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

View File

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

View File

@@ -110,7 +110,7 @@ class TagSetPresenter
html = %{<li class="tag-type-#{tag.category}" data-tag-name="#{h(name)}">} html = %{<li class="tag-type-#{tag.category}" data-tag-name="#{h(name)}">}
unless name_only 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> } html << %{<a class="wiki-link" href="/artists/show_or_new?name=#{u(name)}">?</a> }
elsif name =~ /\A\d+\z/ elsif name =~ /\A\d+\z/
html << %{<a class="wiki-link" href="/wiki_pages/~#{u(name)}">?</a> } html << %{<a class="wiki-link" href="/wiki_pages/~#{u(name)}">?</a> }
@@ -125,7 +125,7 @@ class TagSetPresenter
end end
humanized_tag = humanize_tags ? name.tr("_", " ") : name 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> } html << %{<a class="search-tag" #{itemprop} href="/posts?tags=#{u(name)}">#{h(humanized_tag)}</a> }
unless name_only || tag.new_record? unless name_only || tag.new_record?
@@ -137,7 +137,7 @@ class TagSetPresenter
post_count = count post_count = count
end 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" : ""}" klass = "post-count#{is_underused_tag ? " low-post-count" : ""}"
html << %{<span class="#{klass}" title="#{count}">#{post_count}</span>} 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"> <div id="wiki-page-posts">
<h2>Recent Posts <%= link_to "»", posts_path(tags: wiki_page.title) %></h2> <h2>Recent Posts <%= link_to "»", posts_path(tags: wiki_page.title) %></h2>