From 6c07ab3065a6e57b4fdf7cdea0e9ef1ff9358919 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Thu, 24 Oct 2024 04:10:07 -0500 Subject: [PATCH] Make artist search work with external images --- app/models/api_image.rb | 8 +++++++- .../yiff_rest/api_v2/images/_external_image.html.erb | 2 +- app/views/yiff_rest/api_v2/images/_image.html.erb | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/models/api_image.rb b/app/models/api_image.rb index 5b5e855..7a281be 100644 --- a/app/models/api_image.rb +++ b/app/models/api_image.rb @@ -98,12 +98,18 @@ class APIImage < ApplicationRecord where(id: APIImage.from("unnest(artists) AS artist").where("artist LIKE ?", name.to_escaped_for_sql_like)) end + def external_artists_like(name) + where(id: ExternalAPIImage.from("external_api_images, jsonb_array_elements_text(cached_data->'tags'->'artist') AS artist").where("artist LIKE ?", name.to_escaped_for_sql_like).select("api_image_id")) + end + def search(params) q = super q = q.attribute_matches(:category, params[:category]) q = q.attribute_matches(:original_url, params[:original_url]) q = q.attribute_matches(:is_viewable, params[:viewable]) - q = q.artists_like(params[:artist]) if params[:artist].present? + if params[:artist].present? # rubocop:disable Style/IfUnlessModifier + q = q.artists_like(params[:artist]).or(external_artists_like(params[:artist])) + end if params[:md5].present? # rubocop:disable Style/IfUnlessModifier q = q.attribute_matches(:id, params[:md5]).or(q.where(id: ExternalAPIImage.where("cached_data->'file'->>'md5' = ?", params[:md5]).select("api_image_id"))) end diff --git a/app/views/yiff_rest/api_v2/images/_external_image.html.erb b/app/views/yiff_rest/api_v2/images/_external_image.html.erb index 0b6896e..df24b93 100644 --- a/app/views/yiff_rest/api_v2/images/_external_image.html.erb +++ b/app/views/yiff_rest/api_v2/images/_external_image.html.erb @@ -9,7 +9,7 @@ <% end %> <% end %> -<%= external.artists.join(", ") %> +<%= external.artists.map { |art| link_to(art, yiff_rest_api_v2_manage_images_path(search: @sp.merge(artist: art))) }.join(", ").html_safe %>