From ad3af75287f748f14b1398f6b0e93582a6892f6b Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Tue, 7 May 2024 01:57:00 -0500 Subject: [PATCH] Add more search options for image ui --- app/controllers/yiff_rest/api_v2/images_controller.rb | 2 +- app/models/api_image.rb | 8 ++++++++ app/models/application_record.rb | 5 +++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/controllers/yiff_rest/api_v2/images_controller.rb b/app/controllers/yiff_rest/api_v2/images_controller.rb index c9793d2..737618f 100644 --- a/app/controllers/yiff_rest/api_v2/images_controller.rb +++ b/app/controllers/yiff_rest/api_v2/images_controller.rb @@ -62,7 +62,7 @@ module YiffRest end def search_images_params - permit_search_params(%i[category]) + permit_search_params(%i[category md5 original_url]) end def create_params diff --git a/app/models/api_image.rb b/app/models/api_image.rb index d73b33a..ebd884c 100644 --- a/app/models/api_image.rb +++ b/app/models/api_image.rb @@ -67,6 +67,14 @@ class APIImage < ApplicationRecord end data end + + def search(params) + q = super + params[:id] ||= params[:md5] + q = q.attribute_matches(:category, params[:category]) + q = q.attribute_matches(:original_url, params[:original_url]) + q.order(created_at: :desc) + end end extend SearchMethods diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 78fc6f3..d5eab05 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -58,8 +58,10 @@ class ApplicationRecord < ActiveRecord::Base numeric_attribute_matches(attribute, value, **) when :string, :text text_attribute_matches(attribute, value, **) + when :uuid + where(attribute => value) else - raise(ArgumentError, "unhandled attribute type") + raise(ArgumentError, "unhandled attribute type: #{column.sql_type_metadata.type}") end end @@ -143,7 +145,6 @@ class ApplicationRecord < ActiveRecord::Base q = all q = q.attribute_matches(:id, params[:id]) - q = q.attribute_matches(:category, params[:category]) q = q.attribute_matches(:created_at, params[:created_at]) if attribute_names.include?("created_at") q = q.attribute_matches(:updated_at, params[:updated_at]) if attribute_names.include?("updated_at")