Skip to content

Commit

Permalink
* Add tests for filename convention
Browse files Browse the repository at this point in the history
* Update document model, uploader and factories
* Add sequence to file uploads.
* these updates are related to issue #40.
* Note: First, middle, last have been swapped for just "name"
  • Loading branch information
asadaqain committed May 10, 2024
1 parent 4c1716f commit 47f0bda
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ RUN if [ ! -f Gemfile ] ; then gem install rails ; fi
ADD https://raw.githubusercontent.com/yorkulibraries/docker-rails/main/rt.sh /usr/local/bin/rt
RUN chmod a+x /usr/local/bin/rt

ADD https://raw.githubusercontent.com/yorkulibraries/docker-rails/main/rts.sh /usr/local/bin/rts
RUN chmod a+x /usr/local/bin/rts
17 changes: 11 additions & 6 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ def filename_naming
errors.add(:file, 'Primary have to be a PDF')
end

def self.filename_by_convention(first_name, last_name, middle_name, exam_date, degree_level, filename)
last_and_first_name = "#{last_name.titleize}_#{first_name.titleize}".gsub(/\s/, '_')
def self.filename_by_convention(name, exam_date, degree_name, degree_level, filename, is_supplemental, usage)
full_name = "#{name.titleize}".gsub(/\s/, '_')
year_copy = exam_date.year
grade = degree_level == 'Doctoral' ? 'PhD' : 'Masters'
year_grade_and_ext = "#{year_copy}_#{grade}#{File.extname(filename)}"
level = degree_level == 'Doctoral' ? 'PhD' : 'Masters'
deg_name = degree_name.gsub(/\s/, '_')
year_degree_and_ext = "#{year_copy}_#{deg_name}_#{level}"

middle_name.nil? ? "#{last_and_first_name}_#{year_grade_and_ext}"
: "#{last_and_first_name}_#{middle_name.titleize}_#{year_grade_and_ext}"
sequence_number = self.count
upload_type = is_supplemental ? "Supplemental" : "Primary"
usage == nil ? usage_caps = '' : usage_caps = usage.upcase + "_FILE"

"#{full_name}_#{year_degree_and_ext}_#{usage_caps}_#{upload_type}_#{sequence_number}#{File.extname(filename)}"
end

end
4 changes: 2 additions & 2 deletions app/uploaders/document_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ 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.last_name.nil?
return original_filename unless model.user.present? && !model.user.name.nil?
return original_filename if model.usage != "thesis"

Document.filename_by_convention(model.user.first_name, model.user.last_name, model.user.middle_name, model.thesis.exam_date, model.thesis.degree_level, original_filename)
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
Expand Down
8 changes: 8 additions & 0 deletions test/factories/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@
association :thesis, factory: :thesis
association :user, factory: :student_with_last_name
end

factory :document_for_file_naming, class: Document do
sequence(:name) { |n| "document #{n}" }
supplemental true
association :thesis, factory: :thesis
association :user, factory: :student
end

end
37 changes: 33 additions & 4 deletions test/models/document_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,42 @@ class DocumentTest < ActiveSupport::TestCase
assert d.image?, 'This one should be an image'
end

should 'automatic naming convention ' do
document_name = 'html-document.html'
d = create(:document_for_naming, file: fixture_file_upload(document_name, 'text/html'), usage: :thesis)
expected = Document.filename_by_convention(d.user.first_name, d.user.last_name, d.user.middle_name, d.thesis.exam_date, d.thesis.degree_level, document_name)
should 'automatic naming for primary file FullNameField_Year_Degree_Primary.pdf' do
document_name = 'pdf-document.pdf'
d = create(:document_for_file_naming, file: fixture_file_upload(document_name), usage: :thesis, supplemental: false)
expected = Document.filename_by_convention(d.user.name, d.thesis.exam_date, d.thesis.degree_name, d.thesis.degree_level, document_name, d.supplemental, d.usage)

assert_equal expected, d.file.filename
end

should 'automate naming for secondary file FullNameField_Year_Degree_Supplementary_1.file extension' do
document_name = 'pdf-document.pdf'
d = create(:document_for_file_naming, file: fixture_file_upload(document_name), usage: :thesis, supplemental: true)
expected = Document.filename_by_convention(d.user.name, d.thesis.exam_date, d.thesis.degree_name, d.thesis.degree_level, document_name, d.supplemental, d.usage)

assert_equal expected, d.file.filename

end

should 'sequence document attachments in filenames' do

document_name = 'pdf-document.pdf'
dp1 = create(:document_for_file_naming, file: fixture_file_upload(document_name), usage: :thesis, supplemental: false)
# Assert that the filename ends with "_1"
assert_match(/_1\.\w+$/, dp1.file.filename)

supplemental_name = 'image-example.jpg'
dsp1 = create(:document_for_file_naming, file: fixture_file_upload(supplemental_name), usage: :thesis, supplemental: true)
# Assert that the filename ends with "_2"
assert_match(/_2\.\w+$/, dsp1.file.filename)

supplemental_name_2 = 'papyrus-feature.png'
dsp2 = create(:document_for_file_naming, file: fixture_file_upload(supplemental_name_2), usage: :thesis, supplemental: true)
# Assert that the filename ends with "_3"
assert_match(/_3\.\w+$/, dsp2.file.filename)

end

should 'None naming convention for non-existence of usage' do
document_name = 'html-document.html'
d = create(:document_for_naming, file: fixture_file_upload(document_name, 'text/html'))
Expand Down

0 comments on commit 47f0bda

Please sign in to comment.