artists: prevent creating artist entries for non-artist tags (#4107, 2717).

Validate that artist entries belong to an artist tag. Don't allow
creating artist entries for character/copyright/meta tags, but do allow
entries for general tags (the gentag will be automatically changed to an
artist tag).
This commit is contained in:
evazion
2019-08-01 20:11:27 -05:00
parent bcaefa0a7e
commit 065609ff4f
2 changed files with 37 additions and 17 deletions

View File

@@ -8,9 +8,9 @@ class Artist < ApplicationRecord
before_validation :normalize_name before_validation :normalize_name
before_validation :normalize_other_names before_validation :normalize_other_names
after_save :create_version after_save :create_version
after_save :categorize_tag
after_save :update_wiki after_save :update_wiki
after_save :clear_url_string_changed after_save :clear_url_string_changed
validate :validate_tag_category
validates :name, tag_name: true, uniqueness: true validates :name, tag_name: true, uniqueness: true
belongs_to_creator belongs_to_creator
has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name" has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name"
@@ -18,7 +18,7 @@ class Artist < ApplicationRecord
has_many :versions, -> {order("artist_versions.id ASC")}, :class_name => "ArtistVersion" has_many :versions, -> {order("artist_versions.id ASC")}, :class_name => "ArtistVersion"
has_one :wiki_page, :foreign_key => "title", :primary_key => "name" has_one :wiki_page, :foreign_key => "title", :primary_key => "name"
has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name" has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name"
has_one :tag, :foreign_key => "name", :primary_key => "name" belongs_to :tag, foreign_key: "name", primary_key: "name", default: -> { Tag.new(name: name, category: Tag.categories.artist) }
attribute :notes, :string attribute :notes, :string
scope :active, -> { where(is_active: true) } scope :active, -> { where(is_active: true) }
@@ -405,9 +405,13 @@ class Artist < ApplicationRecord
Tag.category_for(name) Tag.category_for(name)
end end
def categorize_tag def validate_tag_category
if new_record? || saved_change_to_name? return unless is_active? && name_changed?
Tag.find_or_create_by_name("artist:#{name}")
if tag.category_name == "General"
tag.update(category: Tag.categories.artist)
elsif tag.category_name != "Artist"
errors[:base] << "'#{name}' is a #{tag.category_name.downcase} tag; artist entries can only be created for artist tags"
end end
end end
end end

View File

@@ -469,20 +469,36 @@ class ArtistTest < ActiveSupport::TestCase
assert_equal(%w[yyy], artist.other_names) assert_equal(%w[yyy], artist.other_names)
end end
should "update the category of the tag when created" do context "when creating" do
tag = FactoryBot.create(:tag, :name => "abc") should "create a new artist tag if one does not already exist" do
artist = FactoryBot.create(:artist, :name => "abc") FactoryBot.create(:artist, name: "bkub")
tag.reload assert(Tag.exists?(name: "bkub", category: Tag.categories.artist))
assert_equal(Tag.categories.artist, tag.category) end
should "change the tag to an artist tag if it was a gentag" do
tag = FactoryBot.create(:tag, name: "abc", category: Tag.categories.general)
artist = FactoryBot.create(:artist, name: "abc")
assert_equal(Tag.categories.artist, tag.reload.category)
end
should "not allow creating artist entries for non-artist tags" do
tag = FactoryBot.create(:tag, name: "touhou", category: Tag.categories.copyright)
artist = FactoryBot.build(:artist, name: "touhou")
assert(artist.invalid?)
assert_match(/'touhou' is a copyright tag/, artist.errors.full_messages.join)
end
end end
should "update the category of the tag when renamed" do context "when renaming" do
tag = FactoryBot.create(:tag, :name => "def") should "change the new tag to an artist tag if it was a gentag" do
artist = FactoryBot.create(:artist, :name => "abc") tag = FactoryBot.create(:tag, name: "def", category: Tag.categories.general)
artist.name = "def" artist = FactoryBot.create(:artist, name: "abc")
artist.save artist.update(name: "def")
tag.reload
assert_equal(Tag.categories.artist, tag.category) assert_equal(Tag.categories.artist, tag.reload.category)
end
end end
context "when saving" do context "when saving" do