This commit is contained in:
r888888888
2018-05-26 12:58:19 -07:00
parent c054784302
commit ca842cc6d9
4 changed files with 27 additions and 7 deletions

View File

@@ -180,8 +180,13 @@ class Artist < ApplicationRecord
def save_urls def save_urls
if url_string && saved_change_to_url_string? if url_string && saved_change_to_url_string?
self.urls = url_string.scan(/[^[:space:]]+/).uniq.map do |url| Artist.transaction do
self.urls.find_or_create_by(url: url) self.urls = url_string.scan(/[^[:space:]]+/).uniq.map do |url|
# need to do these shenanigans to properly handle prefixes
aurl = self.urls.find_or_create_by(url: ArtistUrl.strip_prefixes(url))
aurl.update(url: url)
aurl
end
end end
end end
end end

View File

@@ -1,11 +1,19 @@
class ArtistUrl < ApplicationRecord class ArtistUrl < ApplicationRecord
before_validation :parse_prefix before_validation :parse_prefix
before_save :initialize_normalized_url, on: [ :create ] before_validation :initialize_normalized_url, on: :create
before_save :normalize before_validation :normalize
validates :url, presence: true validates :url, presence: true, uniqueness: { scope: :artist_id }
validate :validate_url_format validate :validate_url_format
belongs_to :artist, :touch => true belongs_to :artist, :touch => true
def self.strip_prefixes(url)
url.sub(/^[-]+/, "")
end
def self.is_active?(url)
url !~ /^-/
end
def self.normalize(url) def self.normalize(url)
if url.nil? if url.nil?
nil nil
@@ -60,7 +68,8 @@ class ArtistUrl < ApplicationRecord
end end
def parse_prefix def parse_prefix
if url && url[0] == "-" case url
when /^-/
self.url = url[1..-1] self.url = url[1..-1]
self.is_active = false self.is_active = false
end end

View File

@@ -1,6 +1,5 @@
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
var tags = $("#tags").val();
$.post("<%= Danbooru.config.reportbooru_server %>/missed_searches", { $.post("<%= Danbooru.config.reportbooru_server %>/missed_searches", {
sig: "<%= sig %>" sig: "<%= sig %>"
}); });

View File

@@ -42,6 +42,13 @@ class ArtistTest < ActiveSupport::TestCase
refute(@artist.urls[0].is_active?) refute(@artist.urls[0].is_active?)
end end
should "allow deactivating a url" do
@artist = Artist.create(name: "blah", url_string: "http://monet.com")
@artist.update(url_string: "-http://monet.com")
assert_equal(1, @artist.urls.count)
refute(@artist.urls[0].is_active?)
end
should "should have a valid name" do should "should have a valid name" do
@artist = Artist.new(:name => "-blah") @artist = Artist.new(:name => "-blah")
@artist.save @artist.save