artist urls: normalize trailing slashes and missing http://.
* Remove unnecessary trailing slashes when artist URLs are saved. * Automatically add `http://` to new artist URLs if it's missing (before this was an error; now it's automatically fixed).
This commit is contained in:
@@ -72,8 +72,7 @@ class ArtistURL < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def domain
|
def domain
|
||||||
uri = Addressable::URI.parse(normalized_url)
|
Danbooru::URL.parse(normalized_url)&.domain
|
||||||
uri.domain
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def site_name
|
def site_name
|
||||||
@@ -119,11 +118,7 @@ class ArtistURL < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.normalize_url(url)
|
def self.normalize_url(url)
|
||||||
uri = Addressable::URI.parse(url)
|
Danbooru::URL.parse(url)&.to_s.presence || url
|
||||||
uri.site = uri.normalized_site
|
|
||||||
uri.to_s
|
|
||||||
rescue Addressable::URI::InvalidURIError
|
|
||||||
url
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def url=(url)
|
def url=(url)
|
||||||
|
|||||||
@@ -580,7 +580,8 @@ class ArtistTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
should "not save invalid urls" do
|
should "not save invalid urls" do
|
||||||
assert_no_difference("ArtistVersion.count") do
|
assert_no_difference("ArtistVersion.count") do
|
||||||
@artist.update(:url_string => "http://foo.com www.example.com")
|
@artist.update(url_string: "http://foo.com :42")
|
||||||
|
|
||||||
assert_equal(%w[http://foo.com], @artist.versions.last.urls)
|
assert_equal(%w[http://foo.com], @artist.versions.last.urls)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -25,26 +25,29 @@ class ArtistURLTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
should "disallow invalid urls" do
|
should "disallow invalid urls" do
|
||||||
urls = [
|
urls = [
|
||||||
FactoryBot.build(:artist_url, url: "www.example.com"),
|
build(:artist_url, url: ":www.example.com"),
|
||||||
FactoryBot.build(:artist_url, url: ":www.example.com"),
|
build(:artist_url, url: "http://http://www.example.com"),
|
||||||
FactoryBot.build(:artist_url, url: "http://http://www.example.com"),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
assert_equal(false, urls[0].valid?)
|
assert_equal(false, urls[0].valid?)
|
||||||
assert_match(/must begin with http/, urls[0].errors.full_messages.join)
|
assert_match(/is malformed/, urls[0].errors.full_messages.join)
|
||||||
assert_equal(false, urls[1].valid?)
|
assert_equal(false, urls[1].valid?)
|
||||||
assert_match(/is malformed/, urls[1].errors.full_messages.join)
|
assert_match(/that does not contain a dot/, urls[1].errors.full_messages.join)
|
||||||
assert_equal(false, urls[2].valid?)
|
|
||||||
assert_match(/that does not contain a dot/, urls[2].errors.full_messages.join)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
should "always add a trailing slash when normalized" do
|
should "automatically add http:// if missing" do
|
||||||
url = FactoryBot.create(:artist_url, :url => "http://monet.com")
|
url = create(:artist_url, url: "example.com")
|
||||||
|
assert_equal("http://example.com", url.url)
|
||||||
|
assert_equal("http://example.com/", url.normalized_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "normalize trailing slashes" do
|
||||||
|
url = create(:artist_url, url: "http://monet.com")
|
||||||
assert_equal("http://monet.com", url.url)
|
assert_equal("http://monet.com", url.url)
|
||||||
assert_equal("http://monet.com/", url.normalized_url)
|
assert_equal("http://monet.com/", url.normalized_url)
|
||||||
|
|
||||||
url = FactoryBot.create(:artist_url, :url => "http://monet.com/")
|
url = create(:artist_url, url: "http://monet.com/")
|
||||||
assert_equal("http://monet.com/", url.url)
|
assert_equal("http://monet.com", url.url)
|
||||||
assert_equal("http://monet.com/", url.normalized_url)
|
assert_equal("http://monet.com/", url.normalized_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user