artist urls: add more url search params for /artist_urls.

Adds these search params:

* /artist_urls?search[url]=...
* /artist_urls?search[url_eq]=...
* /artist_urls?search[url_not_eq]=...
* /artist_urls?search[url_like]=...
* /artist_urls?search[url_ilike]=...
* /artist_urls?search[url_not_like]=...
* /artist_urls?search[url_not_ilike]=...
* /artist_urls?search[url_regex]=...
* /artist_urls?search[url_not_regex]=...

and likewise for normalized_url.
This commit is contained in:
evazion
2018-09-15 11:45:37 -05:00
parent c06af060f9
commit 3afc0b3a78
2 changed files with 42 additions and 0 deletions

View File

@@ -9,16 +9,32 @@ class ApplicationRecord < ActiveRecord::Base
"#{table_name}.#{column_for_attribute(attr).name}" "#{table_name}.#{column_for_attribute(attr).name}"
end end
def where_like(attr, value)
where("#{qualified_column_for(attr)} LIKE ? ESCAPE E'\\\\'", value.to_escaped_for_sql_like)
end
def where_not_like(attr, value)
where.not("#{qualified_column_for(attr)} LIKE ? ESCAPE E'\\\\'", value.to_escaped_for_sql_like)
end
def where_ilike(attr, value) def where_ilike(attr, value)
where("lower(#{qualified_column_for(attr)}) LIKE ? ESCAPE E'\\\\'", value.mb_chars.downcase.to_escaped_for_sql_like) where("lower(#{qualified_column_for(attr)}) LIKE ? ESCAPE E'\\\\'", value.mb_chars.downcase.to_escaped_for_sql_like)
end end
def where_not_ilike(attr, value)
where.not("lower(#{qualified_column_for(attr)}) LIKE ? ESCAPE E'\\\\'", value.mb_chars.downcase.to_escaped_for_sql_like)
end
# https://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP # 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. # "(?e)" means force use of ERE syntax; see sections 9.7.3.1 and 9.7.3.4.
def where_regex(attr, value) def where_regex(attr, value)
where("#{qualified_column_for(attr)} ~ ?", "(?e)" + value) where("#{qualified_column_for(attr)} ~ ?", "(?e)" + value)
end end
def where_not_regex(attr, value)
where.not("#{qualified_column_for(attr)} ~ ?", "(?e)" + value)
end
def attribute_matches(attribute, value, **options) def attribute_matches(attribute, value, **options)
return all if value.nil? return all if value.nil?
@@ -69,6 +85,30 @@ class ApplicationRecord < ActiveRecord::Base
end end
end end
def search_text_attribute(attr, params, **options)
if params[attr].present?
where(attr => params[attr])
elsif params[:"#{attr}_eq"].present?
where(attr => params[:"#{attr}_eq"])
elsif params[:"#{attr}_not_eq"].present?
where.not(attr => params[:"#{attr}_not_eq"])
elsif params[:"#{attr}_like"].present?
where_like(attr, params[:"#{attr}_like"])
elsif params[:"#{attr}_ilike"].present?
where_ilike(attr, params[:"#{attr}_ilike"])
elsif params[:"#{attr}_not_like"].present?
where_not_like(attr, params[:"#{attr}_not_like"])
elsif params[:"#{attr}_not_ilike"].present?
where_not_ilike(attr, params[:"#{attr}_not_ilike"])
elsif params[:"#{attr}_regex"].present?
where_regex(attr, params[:"#{attr}_regex"])
elsif params[:"#{attr}_not_regex"].present?
where_not_regex(attr, params[:"#{attr}_not_regex"])
else
all
end
end
def apply_default_order(params) def apply_default_order(params)
if params[:order] == "custom" if params[:order] == "custom"
parse_ids = Tag.parse_helper(params[:id]) parse_ids = Tag.parse_helper(params[:id])

View File

@@ -53,6 +53,8 @@ class ArtistUrl < ApplicationRecord
q = q.attribute_matches(:artist_id, params[:artist_id]) q = q.attribute_matches(:artist_id, params[:artist_id])
q = q.attribute_matches(:is_active, params[:is_active]) q = q.attribute_matches(:is_active, params[:is_active])
q = q.search_text_attribute(:url, params)
q = q.search_text_attribute(:normalized_url, params)
q = q.artist_matches(params[:artist]) q = q.artist_matches(params[:artist])
q = q.url_matches(params[:url_matches]) q = q.url_matches(params[:url_matches])