diff --git a/app/controllers/modqueue_controller.rb b/app/controllers/modqueue_controller.rb index 264f44b06..773b2cae7 100644 --- a/app/controllers/modqueue_controller.rb +++ b/app/controllers/modqueue_controller.rb @@ -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 diff --git a/app/logical/d_text.rb b/app/logical/d_text.rb index 70cfa88d1..c50d43f2e 100644 --- a/app/logical/d_text.rb +++ b/app/logical/d_text.rb @@ -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 diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index c30126d79..dc8deafff 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -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 diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb index fea60b880..7521697d9 100644 --- a/app/logical/sources/strategies/base.rb +++ b/app/logical/sources/strategies/base.rb @@ -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. diff --git a/app/logical/tag_name_validator.rb b/app/logical/tag_name_validator.rb index c890f1695..85c714fde 100644 --- a/app/logical/tag_name_validator.rb +++ b/app/logical/tag_name_validator.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index 4c8d800e5..6ae679bd6 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/models/tag.rb b/app/models/tag.rb index e575ff840..2dc756445 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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 diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index 200bdfde9..87b2260b9 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -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 diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 6c6c70f43..b0ea6b766 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -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 diff --git a/app/presenters/tag_set_presenter.rb b/app/presenters/tag_set_presenter.rb index 34ba56508..605f08020 100644 --- a/app/presenters/tag_set_presenter.rb +++ b/app/presenters/tag_set_presenter.rb @@ -110,7 +110,7 @@ class TagSetPresenter html = %{