From facf819620bb16447af9ae13c54fc6fdaa3a1731 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 8 Feb 2017 22:48:48 -0600 Subject: [PATCH] post.rb: validate newly added tags. Existing tags with invalid names are still permitted. This is to allow for a gradual transition to good tag names. --- app/models/post.rb | 16 ++++++++++++++++ app/models/tag.rb | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index 1e464e1d8..6b5f202e6 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -16,6 +16,7 @@ class Post < ActiveRecord::Base before_validation :remove_parent_loops validates_uniqueness_of :md5, :on => :create validates_inclusion_of :rating, in: %w(s q e), message: "rating must be s, q, or e" + validate :tag_names_are_valid validate :post_is_not_its_own_parent validate :updater_can_change_rating before_save :update_tag_post_counts @@ -1699,6 +1700,21 @@ class Post < ActiveRecord::Base end end end + + def tag_names_are_valid + # only validate new tags; allow invalid names for tags that already exist. + added_tags = tag_array - tag_array_was + new_tags = added_tags - Tag.where(name: added_tags).pluck(:name) + + new_tags.each do |name| + tag = Tag.new(name: name) + tag.valid? + + tag.errors.messages.each do |attribute, messages| + errors[:tag_string] << "tag #{attribute} #{messages.join(';')}" + end + end + end end include FileMethods diff --git a/app/models/tag.rb b/app/models/tag.rb index bb2e5dc93..db72c837f 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -2,7 +2,7 @@ class Tag < ActiveRecord::Base COSINE_SIMILARITY_RELATED_TAG_THRESHOLD = 1000 METATAGS = "-user|user|-approver|approver|commenter|comm|noter|noteupdater|artcomm|-pool|pool|ordpool|-favgroup|favgroup|-fav|fav|ordfav|sub|md5|-rating|rating|-locked|locked|width|height|mpixels|ratio|score|favcount|filesize|source|-source|id|-id|date|age|order|limit|-status|status|tagcount|gentags|arttags|chartags|copytags|parent|-parent|child|pixiv_id|pixiv|search|upvote|downvote|filetype|-filetype" SUBQUERY_METATAGS = "commenter|comm|noter|noteupdater|artcomm" - attr_accessible :category, :as => [:moderator, :janitor, :gold, :platinum, :member, :anonymous, :default, :builder, :admin] + attr_accessible :name, :category attr_accessible :is_locked, :as => [:moderator, :admin] has_one :wiki_page, :foreign_key => "title", :primary_key => "name"