evazion
2022-04-03 02:54:30 -05:00
parent 0d480eb832
commit 8ef72d59c1
3 changed files with 29 additions and 10 deletions

View File

@@ -244,18 +244,23 @@ class Artist < ApplicationRecord
end
end
def urls_match(urls)
urls = Array.wrap(urls).flat_map(&:split)
return all if urls.empty?
urls.map do |url|
url_matches(url)
end.reduce(&:or)
end
def url_matches(query)
query = query.strip
if query =~ %r{\A/(.*)/\z}
where(id: ArtistURL.where_regex(:url, $1).select(:artist_id))
elsif query.include?("*")
where(id: ArtistURL.where_like(:url, query).select(:artist_id))
elsif query =~ %r{\Ahttps?://}i
if query =~ %r{\Ahttps?://}i
url = Source::Extractor.find(query).profile_url || query
ArtistFinder.find_artists(url)
else
where(id: ArtistURL.where_like(:url, "*#{query}*").select(:artist_id))
where(id: ArtistURL.url_matches(query).select(:artist_id))
end
end
@@ -285,7 +290,7 @@ class Artist < ApplicationRecord
end
if params[:url_matches].present?
q = q.url_matches(params[:url_matches])
q = q.urls_match(params[:url_matches])
end
case params[:order]

View File

@@ -21,7 +21,7 @@ class ArtistURL < ApplicationRecord
def self.search(params = {})
q = search_attributes(params, :id, :created_at, :updated_at, :url, :is_active, :artist)
q = q.url_matches(params[:url_matches])
q = q.urls_match(params[:url_matches])
case params[:order]
when /\A(id|artist_id|url|is_active|created_at|updated_at)(?:_(asc|desc))?\z/i
@@ -34,6 +34,15 @@ class ArtistURL < ApplicationRecord
q
end
def self.urls_match(urls)
urls = Array.wrap(urls).flat_map(&:split)
return all if urls.empty?
urls.map do |url|
url_matches(url)
end.reduce(&:or)
end
def self.url_matches(url)
if url.blank?
all
@@ -41,9 +50,11 @@ class ArtistURL < ApplicationRecord
where_regex(:url, $1)
elsif url.include?("*")
where_ilike(:url, url)
else
profile_url = Source::Extractor.find(url).profile_url || normalize_url(url)
elsif url =~ %r{\Ahttps?://}i
profile_url = Source::URL.profile_url(url) || Source::Extractor.find(url).profile_url || normalize_url(url)
where(url: profile_url)
else
where_ilike(:url, "*#{url}*")
end
end

View File

@@ -188,6 +188,9 @@ class ArtistURLTest < ActiveSupport::TestCase
assert_search_equals([@bkub_url], url_matches: "*bkub*")
assert_search_equals([@bkub_url], url_matches: "/^https?://bkub\.com$/")
assert_search_equals([@bkub_url], url_matches: "https://bkub.com")
assert_search_equals([@masao_url, @bkub_url], url_matches: "https://bkub.com https://masao.com")
assert_search_equals([@masao_url, @bkub_url], url_matches: ["https://bkub.com", "https://masao.com"])
assert_search_equals([@bkub_url], url: "https://bkub.com")
assert_search_equals([@bkub_url], url_eq: "https://bkub.com")