From 7708e2e08f9c3fe8a254ebb5a0e319b72fd21313 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 19 Dec 2020 14:42:49 -0600 Subject: [PATCH] wikis: don't allow adding other names to artist wikis. Prevent users from adding other names to artist wikis. These should be added to the artist entry instead. --- app/models/wiki_page.rb | 8 ++++++++ app/views/wiki_pages/_form.html.erb | 5 ++++- test/unit/wiki_page_test.rb | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 3b0b28045..a60e10863 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -7,10 +7,12 @@ class WikiPage < ApplicationRecord before_save :normalize_other_names before_save :update_dtext_links, if: :dtext_links_changed? after_save :create_version + validates_uniqueness_of :title, :case_sensitive => false validates_presence_of :title validates_presence_of :body, :unless => -> { is_deleted? || other_names.present? } validate :validate_rename + validate :validate_other_names array_attribute :other_names has_one :tag, :foreign_key => "name", :primary_key => "title" @@ -124,6 +126,12 @@ class WikiPage < ApplicationRecord end end + def validate_other_names + if other_names.present? && tag&.artist? + errors.add(:base, "An artist wiki can't have other names") + end + end + def revert_to(version) if id != version.wiki_page_id raise RevertError.new("You cannot revert to a previous version of another wiki page.") diff --git a/app/views/wiki_pages/_form.html.erb b/app/views/wiki_pages/_form.html.erb index 8f5616dc7..2ff231fd8 100644 --- a/app/views/wiki_pages/_form.html.erb +++ b/app/views/wiki_pages/_form.html.erb @@ -3,7 +3,10 @@ <%= edit_form_for(@wiki_page, url: wiki_page_path(@wiki_page.id)) do |f| %> <%= f.input :title, error: false, input_html: { data: { autocomplete: "tag" } }, hint: "Change to rename this wiki page. Update any wikis linking to this page first." %> - <%= f.input :other_names_string, as: :text, input_html: { size: "30x1" }, label: "Other names (#{link_to_wiki "help", "help:translated_tags"})".html_safe, hint: "Names used for this tag on other sites such as Pixiv. Separate with spaces." %> + + <% if !@wiki_page.tag&.artist? || @wiki_page.other_names.present? %> + <%= f.input :other_names_string, as: :text, input_html: { size: "30x1" }, label: "Other names (#{link_to_wiki "help", "help:translated_tags"})".html_safe, hint: "Names used for this tag on other sites such as Pixiv. Separate with spaces." %> + <% end %> <%= f.input :body, as: :dtext %> diff --git a/test/unit/wiki_page_test.rb b/test/unit/wiki_page_test.rb index bd52fc719..41b814a8a 100644 --- a/test/unit/wiki_page_test.rb +++ b/test/unit/wiki_page_test.rb @@ -77,5 +77,15 @@ class WikiPageTest < ActiveSupport::TestCase assert_equal(0, @wiki_page.dtext_links.size) end end + + context "with other names" do + should "not allow artist wikis to have other names" do + tag = create(:artist_tag) + wiki = build(:wiki_page, title: tag.name, other_names: ["blah"]) + + assert_equal(false, wiki.valid?) + assert_equal(["An artist wiki can't have other names"], wiki.errors[:base]) + end + end end end