artists: improve prefilling of new artist form (#4028)

* When creating an artist by clicking the '?' next to the artist tag in
  the tag list, prefill the new artist form by finding the artist's last
  upload and fetching its source data.

  Previously we filled the urls with the source of the artist's last
  upload, which was wrong because it was usually a direct image URL (#3078).

* Fix the other names field not escaping spaces within names to underscores.

* Fix the other names field being potentially prefilled with duplicate names.
This commit is contained in:
evazion
2018-12-27 15:03:11 -06:00
parent 286bf2f285
commit 2170961f47
5 changed files with 52 additions and 11 deletions

View File

@@ -310,17 +310,26 @@ class Artist < ApplicationRecord
end
module FactoryMethods
# Make a new artist, fetching the defaults either from the given source, or
# from the source of the artist's last upload.
def new_with_defaults(params)
Artist.new(params).tap do |artist|
if artist.name.present?
post = CurrentUser.without_safe_mode do
Post.tag_match("source:http #{artist.name}").where("true /* Artist.new_with_defaults */").first
end
unless post.nil? || post.source.blank?
artist.url_string = post.source
end
source = params.delete(:source)
if source.blank? && params[:name].present?
CurrentUser.without_safe_mode do
post = Post.tag_match("source:http* #{params[:name]}").first
source = post.try(:source)
end
end
if source.present?
artist = Sources::Strategies.find(source).new_artist
artist.attributes = params
else
artist = Artist.new(params)
end
artist.tap(&:validate) # run before_validation callbacks to normalize the names
end
end