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
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) def url_matches(query)
query = query.strip query = query.strip
if query =~ %r{\A/(.*)/\z} if query =~ %r{\Ahttps?://}i
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
url = Source::Extractor.find(query).profile_url || query url = Source::Extractor.find(query).profile_url || query
ArtistFinder.find_artists(url) ArtistFinder.find_artists(url)
else else
where(id: ArtistURL.where_like(:url, "*#{query}*").select(:artist_id)) where(id: ArtistURL.url_matches(query).select(:artist_id))
end end
end end
@@ -285,7 +290,7 @@ class Artist < ApplicationRecord
end end
if params[:url_matches].present? if params[:url_matches].present?
q = q.url_matches(params[:url_matches]) q = q.urls_match(params[:url_matches])
end end
case params[:order] case params[:order]

View File

@@ -21,7 +21,7 @@ class ArtistURL < ApplicationRecord
def self.search(params = {}) def self.search(params = {})
q = search_attributes(params, :id, :created_at, :updated_at, :url, :is_active, :artist) 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] case params[:order]
when /\A(id|artist_id|url|is_active|created_at|updated_at)(?:_(asc|desc))?\z/i when /\A(id|artist_id|url|is_active|created_at|updated_at)(?:_(asc|desc))?\z/i
@@ -34,6 +34,15 @@ class ArtistURL < ApplicationRecord
q q
end 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) def self.url_matches(url)
if url.blank? if url.blank?
all all
@@ -41,9 +50,11 @@ class ArtistURL < ApplicationRecord
where_regex(:url, $1) where_regex(:url, $1)
elsif url.include?("*") elsif url.include?("*")
where_ilike(:url, url) where_ilike(:url, url)
else elsif url =~ %r{\Ahttps?://}i
profile_url = Source::Extractor.find(url).profile_url || normalize_url(url) profile_url = Source::URL.profile_url(url) || Source::Extractor.find(url).profile_url || normalize_url(url)
where(url: profile_url) where(url: profile_url)
else
where_ilike(:url, "*#{url}*")
end end
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: "*bkub*")
assert_search_equals([@bkub_url], url_matches: "/^https?://bkub\.com$/") 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: "https://bkub.com")
assert_search_equals([@bkub_url], url_eq: "https://bkub.com") assert_search_equals([@bkub_url], url_eq: "https://bkub.com")