diff --git a/app/models/artist.rb b/app/models/artist.rb index b9e999c1d..a282b2200 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -180,8 +180,13 @@ class Artist < ApplicationRecord def save_urls if url_string && saved_change_to_url_string? - self.urls = url_string.scan(/[^[:space:]]+/).uniq.map do |url| - self.urls.find_or_create_by(url: url) + Artist.transaction do + 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 diff --git a/app/models/artist_url.rb b/app/models/artist_url.rb index 066c27c27..e0f123de9 100644 --- a/app/models/artist_url.rb +++ b/app/models/artist_url.rb @@ -1,11 +1,19 @@ class ArtistUrl < ApplicationRecord before_validation :parse_prefix - before_save :initialize_normalized_url, on: [ :create ] - before_save :normalize - validates :url, presence: true + before_validation :initialize_normalized_url, on: :create + before_validation :normalize + validates :url, presence: true, uniqueness: { scope: :artist_id } validate :validate_url_format belongs_to :artist, :touch => true + def self.strip_prefixes(url) + url.sub(/^[-]+/, "") + end + + def self.is_active?(url) + url !~ /^-/ + end + def self.normalize(url) if url.nil? nil @@ -60,7 +68,8 @@ class ArtistUrl < ApplicationRecord end def parse_prefix - if url && url[0] == "-" + case url + when /^-/ self.url = url[1..-1] self.is_active = false end diff --git a/app/views/posts/partials/index/_missed_search_count.html.erb b/app/views/posts/partials/index/_missed_search_count.html.erb index abe3f46e9..18ab70d35 100644 --- a/app/views/posts/partials/index/_missed_search_count.html.erb +++ b/app/views/posts/partials/index/_missed_search_count.html.erb @@ -1,6 +1,5 @@