add more intelligent js for artist forms

This commit is contained in:
Albert Yi
2016-10-18 14:53:44 -07:00
parent 2424f24fcd
commit 2a5343b8cf
7 changed files with 45 additions and 37 deletions

View File

@@ -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 href='/wiki_pages/" + escape($("#artist_name").val()) + "'>A wiki page with this name already exists</a>. 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 = $("<a>").attr("href", "/artists/" + artist.id).text(artist.name);
$("#check-name-result").append(link);
});
} else {
$("#check-name-result").text("OK");
}
}
);
e.preventDefault();
});
}
})();

View File

@@ -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," +

View File

@@ -29,10 +29,6 @@ div#c-artists, div#excerpt {
.hint {
display: block;
}
#check-name-result a {
margin-right: 1em;
}
}
div.recent-posts {

View File

@@ -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

View File

@@ -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

View File

@@ -3,13 +3,13 @@
<label for="artist_name">Name</label>
<% if @artist.new_record? %>
<%= text_field "artist", "name" %>
[<%= link_to "check", "#", :id => "check-name-link" %>]
<span id="check-name-result"></span>
<% elsif CurrentUser.user.is_builder? %>
<%= text_field "artist", "name" %>
[<%= link_to "check", "#", :id => "check-name-link" %>]
<span id="check-name-result"></span>
<span class="hint">Change to rename this artist entry and its wiki page</span>
<% else %>
<p><%= @artist.name %></p>
<% end %>

View File

@@ -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")