[Cleanup] Remove where_regex

Only artist_url matching was using this.
This commit is contained in:
Earlopain 2022-10-13 18:22:07 +02:00
parent aaf37cacb0
commit e7b155cb85
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
5 changed files with 23 additions and 42 deletions

View File

@ -25,12 +25,6 @@ class ApplicationRecord < ActiveRecord::Base
where.not("lower(#{qualified_column_for(attr)}) LIKE ? ESCAPE E'\\\\'", value.downcase.to_escaped_for_sql_like)
end
# https://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP
# "(?e)" means force use of ERE syntax; see sections 9.7.3.1 and 9.7.3.4.
def where_regex(attr, value)
where("#{qualified_column_for(attr)} ~ ?", "(?e)" + value)
end
def attribute_exact_matches(attribute, value, **options)
return all unless value.present?

View File

@ -182,7 +182,7 @@ class Artist < ApplicationRecord
while artists.empty? && url.length > 10
u = url.sub(/\/+$/, "") + "/"
u = u.to_escaped_for_sql_like.gsub(/\*/, '%') + '%'
artists += Artist.joins(:urls).where(["artists.is_active = TRUE AND artist_urls.normalized_url LIKE ? ESCAPE E'\\\\'", u]).limit(10).order("artists.name").all
artists += Artist.joins(:urls).where(["artists.is_active = TRUE AND artist_urls.normalized_url ILIKE ? ESCAPE E'\\\\'", u]).limit(10).order("artists.name").all
url = File.dirname(url) + "/"
break if url =~ SITE_BLACKLIST_REGEXP
@ -423,24 +423,16 @@ class Artist < ApplicationRecord
end
def any_name_matches(query)
if query =~ %r!\A/(.*)/\z!
where_regex(:name, $1).or(any_other_name_matches($1)).or(where_regex(:group_name, $1))
else
normalized_name = normalize_name(query)
normalized_name = "*#{normalized_name}*" unless normalized_name.include?("*")
where_like(:name, normalized_name).or(any_other_name_like(normalized_name)).or(where_like(:group_name, normalized_name))
end
normalized_name = normalize_name(query)
normalized_name = "*#{normalized_name}*" unless normalized_name.include?("*")
where_like(:name, normalized_name).or(any_other_name_like(normalized_name)).or(where_like(:group_name, normalized_name))
end
def url_matches(query)
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
find_artists(query)
else
where(id: ArtistUrl.where_like(:url, "*#{query}*").select(:artist_id))
where(id: ArtistUrl.search(url_matches: query).select(:artist_id))
end
end

View File

@ -65,12 +65,9 @@ class ArtistUrl < ApplicationRecord
def self.url_attribute_matches(attr, url)
if url.blank?
all
elsif url =~ %r!\A/(.*)/\z!
where_regex(attr, $1)
elsif url.include?("*")
where_ilike(attr, url)
else
where(attr => normalize(url))
url = "*#{url}*" if url.exclude?("*")
where_ilike(attr, url)
end
end

View File

@ -197,33 +197,33 @@ class ArtistTest < ActiveSupport::TestCase
end
should "search on its name should return results" do
artist = FactoryBot.create(:artist, :name => "artist")
FactoryBot.create(:artist, name: "artist")
assert_not_nil(Artist.search(:name => "artist").first)
assert_not_nil(Artist.search(:name_like => "artist").first)
assert_not_nil(Artist.search(:any_name_matches => "artist").first)
assert_not_nil(Artist.search(:any_name_matches => "/art/").first)
assert_not_nil(Artist.search(name: "artist").first)
assert_not_nil(Artist.search(name_like: "artist").first)
assert_not_nil(Artist.search(any_name_matches: "artist").first)
assert_not_nil(Artist.search(any_name_matches: "*art*").first)
end
should "search on other names should return matches" do
artist = FactoryBot.create(:artist, :name => "artist", :other_names_string => "aaa ccc_ddd")
FactoryBot.create(:artist, name: "artist", other_names_string: "aaa ccc_ddd")
assert_nil(Artist.search(any_other_name_like: "*artist*").first)
assert_not_nil(Artist.search(any_other_name_like: "*aaa*").first)
assert_not_nil(Artist.search(any_other_name_like: "*ccc_ddd*").first)
assert_not_nil(Artist.search(name: "artist").first)
assert_not_nil(Artist.search(:any_name_matches => "aaa").first)
assert_not_nil(Artist.search(:any_name_matches => "/a/").first)
assert_not_nil(Artist.search(any_name_matches: "aaa").first)
assert_not_nil(Artist.search(any_name_matches: "*a*").first)
end
should "search on group name and return matches" do
cat_or_fish = FactoryBot.create(:artist, :name => "cat_or_fish")
yuu = FactoryBot.create(:artist, :name => "yuu", :group_name => "cat_or_fish")
cat_or_fish = FactoryBot.create(:artist, name: "cat_or_fish")
FactoryBot.create(:artist, name: "yuu", group_name: "cat_or_fish")
assert_equal("yuu", cat_or_fish.member_names)
assert_not_nil(Artist.search(:group_name => "cat_or_fish").first)
assert_not_nil(Artist.search(:any_name_matches => "cat_or_fish").first)
assert_not_nil(Artist.search(:any_name_matches => "/cat/").first)
assert_not_nil(Artist.search(group_name: "cat_or_fish").first)
assert_not_nil(Artist.search(any_name_matches: "cat_or_fish").first)
assert_not_nil(Artist.search(any_name_matches: "*cat*").first)
end
should "search on url and return matches" do
@ -231,7 +231,7 @@ class ArtistTest < ActiveSupport::TestCase
assert_equal([bkub.id], Artist.search(url_matches: "bkub").map(&:id))
assert_equal([bkub.id], Artist.search(url_matches: "*bkub*").map(&:id))
assert_equal([bkub.id], Artist.search(url_matches: "/rifyu|bkub/").map(&:id))
assert_equal([], Artist.search(url_matches: "*rifyu*").map(&:id))
assert_equal([bkub.id], Artist.search(url_matches: "http://bkub.com/test.jpg").map(&:id))
end

View File

@ -93,11 +93,9 @@ class ArtistUrlTest < ActiveSupport::TestCase
assert_search_equals([@bkub_url], artist: { name: "bkub" })
assert_search_equals([@bkub_url], url_matches: "*bkub*")
assert_search_equals([@bkub_url], url_matches: "/^https?://bkub\.com$/")
assert_search_equals([@bkub_url], normalized_url_matches: "*bkub*")
assert_search_equals([@bkub_url], normalized_url_matches: "/^https?://bkub\.com/$/")
assert_search_equals([@bkub_url], normalized_url_matches: "https://bkub.com")
assert_search_equals([@bkub_url], normalized_url_matches: "http://bkub.com")
assert_search_equals([@bkub_url], url: "https://bkub.com")
assert_search_equals([@bkub_url], url_eq: "https://bkub.com")