diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 396a472ce..b2f627553 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -9,16 +9,32 @@ class ApplicationRecord < ActiveRecord::Base "#{table_name}.#{column_for_attribute(attr).name}" 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) where("lower(#{qualified_column_for(attr)}) LIKE ? ESCAPE E'\\\\'", value.mb_chars.downcase.to_escaped_for_sql_like) 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 # "(?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 where_not_regex(attr, value) + where.not("#{qualified_column_for(attr)} ~ ?", "(?e)" + value) + end + def attribute_matches(attribute, value, **options) return all if value.nil? @@ -69,6 +85,30 @@ class ApplicationRecord < ActiveRecord::Base 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) if params[:order] == "custom" parse_ids = Tag.parse_helper(params[:id]) diff --git a/app/models/artist_url.rb b/app/models/artist_url.rb index dbeb28fa0..14953b796 100644 --- a/app/models/artist_url.rb +++ b/app/models/artist_url.rb @@ -53,6 +53,8 @@ class ArtistUrl < ApplicationRecord q = q.attribute_matches(:artist_id, params[:artist_id]) 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.url_matches(params[:url_matches])