From f3f9927a23c40461db9b89a6d0961ae5c6189621 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Thu, 1 Aug 2024 11:11:12 +0200 Subject: [PATCH] Add a with_library_path scope to Attachable --- app/models/concerns/spina/attachable.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/spina/attachable.rb b/app/models/concerns/spina/attachable.rb index c7adb916d..ab99b0321 100644 --- a/app/models/concerns/spina/attachable.rb +++ b/app/models/concerns/spina/attachable.rb @@ -11,6 +11,25 @@ module Attachable "%" + Image.sanitize_sql_like(query) + "%" ) end + + scope :with_library_path, ->(query) do + folder_name, file_name = query.split("/") + unless file_name + file_name = folder_name + folder_name = nil + end + + relation = joins(:file_blob).where( + "active_storage_blobs.filename ILIKE ?", + "%" + Image.sanitize_sql_like(file_name) + "%" + ) + + if folder_name + relation.joins(:media_folder).where("spina_media_folders.name = ?", folder_name) + else + relation + end + end end def name @@ -18,4 +37,4 @@ def name end end -end \ No newline at end of file +end