From 475a8b40fdc8029e584ea0fbad49b72a51b8b5e1 Mon Sep 17 00:00:00 2001 From: Ali Sadaqain Date: Tue, 23 Jul 2024 12:11:01 -0400 Subject: [PATCH] Add prefix of usage type to filename --- app/uploaders/document_uploader.rb | 21 +++++++++++++++++---- test/models/document_test.rb | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/app/uploaders/document_uploader.rb b/app/uploaders/document_uploader.rb index 041d500..9b89e65 100644 --- a/app/uploaders/document_uploader.rb +++ b/app/uploaders/document_uploader.rb @@ -28,12 +28,25 @@ def store_dir # Override the filename of the uploaded files: # Avoid using model.id or version_name here, see uploader/store.rb for details. def filename - return original_filename unless model.user.present? && !model.user.name.nil? - return "#{original_filename}" if model.usage != "thesis" - # return "#{model.usage.upcase}_#{original_filename}" if model.usage != "thesis" + + if model.usage == "thesis" + Document.filename_by_convention(model.user.name, model.thesis.exam_date, model.thesis.degree_name, model.thesis.degree_level, original_filename, model.supplemental, model.usage) + elsif model.usage == "licence" || model.usage == "embargo" || model.usage == "embargo_letter" + "#{model.usage.upcase}_#{original_filename}" + elsif model.user.present? && !model.user.name.nil? + original_filename + else + original_filename + end + + + # return original_filename unless model.user.present? && !model.user.name.nil? + # return "#{original_filename}" if model.usage != "thesis" + # return "#{original_filename}" if model.usage != "thesis" + # # return "#{model.usage.upcase}_#{original_filename}" if model.usage != "thesis" ## Therefore, thesis upload - Document.filename_by_convention(model.user.name, model.thesis.exam_date, model.thesis.degree_name, model.thesis.degree_level, original_filename, model.supplemental, model.usage) + end def move_to_cache diff --git a/test/models/document_test.rb b/test/models/document_test.rb index e8a977b..235e54b 100644 --- a/test/models/document_test.rb +++ b/test/models/document_test.rb @@ -110,6 +110,26 @@ class DocumentTest < ActiveSupport::TestCase assert_match(/_Supplemental_.*\.\w+$/, t.documents.first.file.to_s) end + should 'licence files with usage in filename' do + t = create(:thesis) + licence_document = create(:document_for_file_naming, file: fixture_file_upload('html-document.html'), usage: :licence, supplemental: true, thesis: t) + + assert_match(/LICENCE_.*\.\w+$/, t.documents.first.file.to_s) + + end + + should 'embargo file with usage in filename' do + t = create(:thesis) + embargo_document = create(:document_for_file_naming, file: fixture_file_upload('image-example.jpg'), usage: :embargo, supplemental: true, thesis: t) + + assert_match(/EMBARGO_.*\.\w+$/, t.documents.first.file.to_s) + + embargo_letter_document = create(:document_for_file_naming, file: fixture_file_upload('pdf-document.pdf'), usage: :embargo_letter, supplemental: true, thesis: t) + assert_match(/EMBARGO_LETTER.*\.\w+$/, t.documents[1].file.to_s) + + end + + should 'save files in sequence for supplementary' do # t = create(:thesis)