artists: fix editing invalid urls in artist entries (fix #3720, #3927, #3781)

Convert to an autosave association on urls. This ensures that when we
save the artist we only validate the added urls, not bad urls that we're
trying to remove, and that url validation errors are propagated up to
the artist object.

This also fixes invalid urls being saved in the artist history despite
validation failing (#3720).
This commit is contained in:
evazion
2018-10-04 17:38:44 -05:00
parent c78dece411
commit 03cc3dfa50
4 changed files with 49 additions and 45 deletions

View File

@@ -1,5 +1,4 @@
class ArtistUrl < ApplicationRecord
before_validation :parse_prefix
before_validation :initialize_normalized_url, on: :create
before_validation :normalize
validates :url, presence: true, uniqueness: { scope: :artist_id }
@@ -9,12 +8,11 @@ class ArtistUrl < ApplicationRecord
scope :url_matches, ->(url) { url_attribute_matches(:url, url) }
scope :normalized_url_matches, ->(url) { url_attribute_matches(:normalized_url, url) }
def self.strip_prefixes(url)
url.sub(/^[-]+/, "")
end
def self.parse_prefix(url)
prefix, url = url.match(/\A(-)?(.*)/)[1, 2]
is_active = prefix.nil?
def self.is_active?(url)
url !~ /^-/
[is_active, url]
end
def self.normalize(url)
@@ -88,14 +86,6 @@ class ArtistUrl < ApplicationRecord
end
end
def parse_prefix
case url
when /^-/
self.url = url[1..-1]
self.is_active = false
end
end
def priority
if normalized_url =~ /pixiv\.net\/member\.php/
10
@@ -132,8 +122,8 @@ class ArtistUrl < ApplicationRecord
def validate_url_format
uri = Addressable::URI.parse(url)
errors[:url] << "#{uri} must begin with http:// or https://" if !uri.scheme.in?(%w[http https])
errors[:url] << "'#{uri}' must begin with http:// or https:// " if !uri.scheme.in?(%w[http https])
rescue Addressable::URI::InvalidURIError => error
errors[:url] << "is malformed: #{error}"
errors[:url] << "'#{uri}' is malformed: #{error}"
end
end