From 2a5343b8cfd6a209f78b79638980f9e9759596ec Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Tue, 18 Oct 2016 14:53:44 -0700 Subject: [PATCH] add more intelligent js for artist forms --- app/assets/javascripts/artists.js | 50 ++++++++----------- app/assets/javascripts/autocomplete.js | 2 +- .../stylesheets/specific/artists.css.scss | 4 -- app/controllers/wiki_pages_controller.rb | 2 +- app/models/artist.rb | 8 +++ app/views/artists/_form.html.erb | 4 +- test/unit/artist_test.rb | 12 +++++ 7 files changed, 45 insertions(+), 37 deletions(-) diff --git a/app/assets/javascripts/artists.js b/app/assets/javascripts/artists.js index 9100ea0fd..9ea2afa36 100644 --- a/app/assets/javascripts/artists.js +++ b/app/assets/javascripts/artists.js @@ -3,7 +3,7 @@ Danbooru.Artist.initialize_all = function() { if ($("#c-artists").length) { - Danbooru.Artist.initialize_check_name_link(); + Danbooru.Artist.initialize_check_name(); if (Danbooru.meta("enable-auto-complete") === "true") { Danbooru.Artist.initialize_autocomplete(); @@ -11,6 +11,26 @@ } } + Danbooru.Artist.initialize_check_name = function() { + $("#artist_name").keyup(function(e) { + if ($("#artist_name").val().length > 0) { + $("#check-name-result").html(""); + + $.getJSON("/artists?search[name]=" + escape($("#artist_name").val()), function(data) { + if (data.length === 0) { + $.getJSON("/wiki_pages/" + escape($("#artist_name").val()), function(data) { + if (data !== null) { + $("#check-name-result").html("A wiki page with this name already exists. You must either move the wiki page or pick another artist name.") + } + }); + } else { + $("#check-name-result").html("An artist with this name already exists.") + } + }); + } + }); + } + Danbooru.Artist.initialize_autocomplete = function() { var $fields = $("#search_name,#quick_search_name"); @@ -45,34 +65,6 @@ $(field).data("uiAutocomplete")._renderItem = render_artist; }); } - - Danbooru.Artist.initialize_check_name_link = function() { - $("#check-name-link").click(function(e) { - var artist_name = $("#artist_name").val(); - - if (artist_name.length === 0) { - $("#check-name-result").html("OK"); - } - - $.get("/artists.json?name=" + artist_name, - function(artists) { - $("check-name-result").empty(); - - if (artists.length) { - $("#check-name-result").text("Taken: "); - - $.map(artists.slice(0, 5), function (artist) { - var link = $("").attr("href", "/artists/" + artist.id).text(artist.name); - $("#check-name-result").append(link); - }); - } else { - $("#check-name-result").text("OK"); - } - } - ); - e.preventDefault(); - }); - } })(); diff --git a/app/assets/javascripts/autocomplete.js b/app/assets/javascripts/autocomplete.js index 4f58cda3e..0388f2608 100644 --- a/app/assets/javascripts/autocomplete.js +++ b/app/assets/javascripts/autocomplete.js @@ -93,7 +93,7 @@ ); var $fields_single = $( "#c-tags #search_name_matches,#c-tag-aliases #query,#c-tag-implications #query," + - "#wiki_page_title,#artist_name," + + "#wiki_page_title," + "#tag_alias_request_antecedent_name,#tag_alias_request_consequent_name," + "#tag_implication_request_antecedent_name,#tag_implication_request_consequent_name," + "#tag_alias_antecedent_name,#tag_alias_consequent_name," + diff --git a/app/assets/stylesheets/specific/artists.css.scss b/app/assets/stylesheets/specific/artists.css.scss index 270e745f1..f0d433eab 100644 --- a/app/assets/stylesheets/specific/artists.css.scss +++ b/app/assets/stylesheets/specific/artists.css.scss @@ -29,10 +29,6 @@ div#c-artists, div#excerpt { .hint { display: block; } - - #check-name-result a { - margin-right: 1em; - } } div.recent-posts { diff --git a/app/controllers/wiki_pages_controller.rb b/app/controllers/wiki_pages_controller.rb index 1a69de702..383f48c87 100644 --- a/app/controllers/wiki_pages_controller.rb +++ b/app/controllers/wiki_pages_controller.rb @@ -39,7 +39,7 @@ class WikiPagesController < ApplicationController @wiki_page = WikiPage.find(params[:id]) else @wiki_page = WikiPage.find_by_title(params[:id]) - if @wiki_page.nil? + if @wiki_page.nil? && request.format.symbol == :html redirect_to show_or_new_wiki_pages_path(:title => params[:id]) return end diff --git a/app/models/artist.rb b/app/models/artist.rb index 93c22e8cb..3727a2fee 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -8,6 +8,7 @@ class Artist < ActiveRecord::Base after_save :categorize_tag validates_uniqueness_of :name validate :name_is_valid + validate :wiki_is_empty, :on => :create belongs_to :creator, :class_name => "User" has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name" has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl" @@ -252,6 +253,13 @@ class Artist < ActiveRecord::Base def notes_changed? !!@notes_changed end + + def wiki_is_empty + if WikiPage.titled(name).exists? + errors.add(:name, "conflicts with a wiki page") + return false + end + end end module TagMethods diff --git a/app/views/artists/_form.html.erb b/app/views/artists/_form.html.erb index 96f95b2ed..d0a3b8b69 100644 --- a/app/views/artists/_form.html.erb +++ b/app/views/artists/_form.html.erb @@ -3,13 +3,13 @@ <% if @artist.new_record? %> <%= text_field "artist", "name" %> - [<%= link_to "check", "#", :id => "check-name-link" %>] + <% elsif CurrentUser.user.is_builder? %> <%= text_field "artist", "name" %> - [<%= link_to "check", "#", :id => "check-name-link" %>] Change to rename this artist entry and its wiki page + <% else %>

<%= @artist.name %>

<% end %> diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index 1d8ca5432..4726cb0ab 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -101,6 +101,18 @@ class ArtistTest < ActiveSupport::TestCase assert_equal(artist.name, artist.wiki_page.title) end + context "when a wiki page with the same name already exists" do + setup do + @wiki_page = FactoryGirl.create(:wiki_page, :title => "aaa") + @artist = FactoryGirl.build(:artist, :name => "aaa") + end + + should "not validate" do + @artist.save + assert_equal(["Name conflicts with a wiki page"], @artist.errors.full_messages) + end + end + should "update the wiki page when notes are assigned" do artist = FactoryGirl.create(:artist, :name => "aaa", :notes => "testing") artist.update_attribute(:notes, "kokoko")