From 963eacd849692ff4d81196e4a12717dc1e0e8293 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 8 Nov 2017 19:33:02 -0600 Subject: [PATCH] posts: warn when adding newly created tags. --- app/models/post.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index 1a25add2e..ebeee8274 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -18,6 +18,7 @@ class Post < ApplicationRecord 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 :added_tags_are_valid validate :post_is_not_its_own_parent validate :updater_can_change_rating before_save :update_tag_post_counts @@ -592,6 +593,18 @@ class Post < ApplicationRecord @tag_array_was ||= Tag.scan_tags(tag_string_was) end + def tags + Tag.where(name: tag_array) + end + + def tags_was + Tag.where(name: tag_array_was) + end + + def added_tags + tags - tags_was + end + def decrement_tag_post_counts Tag.where(:name => tag_array).update_all("post_count = post_count - 1") if tag_array.any? end @@ -1707,6 +1720,17 @@ class Post < ApplicationRecord end end end + + def added_tags_are_valid + new_tags = added_tags.select { |t| t.post_count <= 1 } + new_general_tags = new_tags.select { |t| t.category == Tag.categories.general } + + if new_general_tags.present? + n = new_general_tags.size + tag_wiki_links = new_general_tags.map { |tag| "[[#{tag.name}]]" } + self.warnings[:base] << "Created #{n} new #{n == 1 ? "tag" : "tags"}: #{tag_wiki_links.join(", ")}" + end + end end include FileMethods