artist urls: add artist, url_matches search params to /artist_urls.

This commit is contained in:
evazion
2018-09-15 11:44:05 -05:00
parent 1fce794b99
commit c06af060f9
3 changed files with 42 additions and 0 deletions

View File

@@ -6,6 +6,9 @@ class ArtistUrl < ApplicationRecord
validate :validate_url_format
belongs_to :artist, :touch => true
scope :url_matches, ->(url) { url_attribute_matches(:url, url) }
scope :normalized_url_matches, ->(url) { url_attribute_matches(:normalized_url, url) }
def self.strip_prefixes(url)
url.sub(/^[-]+/, "")
end
@@ -51,6 +54,10 @@ class ArtistUrl < ApplicationRecord
q = q.attribute_matches(:artist_id, params[:artist_id])
q = q.attribute_matches(:is_active, params[:is_active])
q = q.artist_matches(params[:artist])
q = q.url_matches(params[:url_matches])
q = q.normalized_url_matches(params[:normalized_url_matches])
case params[:order]
when /\A(id|artist_id|url|normalized_url|is_active|created_at|updated_at)(?:_(asc|desc))?\z/i
dir = $2 || :desc
@@ -62,6 +69,22 @@ class ArtistUrl < ApplicationRecord
q
end
def self.artist_matches(params = {})
return all if params.blank?
where(artist_id: Artist.search(params).reorder(nil))
end
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))
end
end
def parse_prefix
case url