Skip to content

Commit

Permalink
[ENHC0010042] Add new file format labels for attachments (#663)
Browse files Browse the repository at this point in the history
* adding new text & spreadsheet pills to attachments

* fixing rubocop error

* fixing test

* adding rtf file extension for labeling text attachments & adding more model tests

* changing the color of text labels
  • Loading branch information
ksierks authored Jul 18, 2024
1 parent 7c6d9f2 commit 7f64695
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/components/viral/pill_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class PillComponent < Viral::Component
red: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300',
green: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300',
yellow: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-300',
orange: 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-300',
indigo: 'bg-indigo-100 text-indigo-800 dark:bg-indigo-900 dark:text-indigo-300',
purple: 'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300',
pink: 'bg-pink-100 text-pink-800 dark:bg-pink-900 dark:text-pink-300',
Expand All @@ -24,6 +25,7 @@ class PillComponent < Viral::Component
red: 'border border-red-800 dark:border-red-300',
green: 'border border-green-800 dark:border-green-300',
yellow: 'border border-yellow-800 dark:border-yellow-300',
orange: 'border border-orange-800 dark:border-orange-300',
indigo: 'border border-indigo-800 dark:border-indigo-300',
purple: 'border border-purple-800 dark:border-purple-300',
pink: 'border border-pink-800 dark:border-pink-300',
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/pill_color_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module PillColorHelper
format: {
fasta: 'blue',
fastq: 'green',
text: 'orange',
spreadsheet: 'yellow',
unknown: 'slate'
},
type: {
Expand Down
8 changes: 7 additions & 1 deletion app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def associated_attachment

private

def assign_metadata
def assign_metadata # rubocop:disable Metrics/AbcSize
return if metadata.key? 'format'

case filename.to_s
Expand All @@ -56,6 +56,12 @@ def assign_metadata
# Assigns fastq to metadata format for following file types: .fastq, .fastq.gz, .fq, .fq.gz
when /^\S+\.f(ast)?q(\.gz)?$/
metadata['format'] = 'fastq'
# Assigns text to metadata format for following file types: .txt, .rtf, .csv, .tsv
when /^\S+\.(txt|rtf|csv|tsv)?$/
metadata['format'] = 'text'
# Assigns spreadsheet to metadata format for following file types: .xls, .xlsx
when /^\S+\.(xls|xlsx)?$/
metadata['format'] = 'spreadsheet'
# Else assigns unknown to metadata format
else
metadata['format'] = 'unknown'
Expand Down
52 changes: 40 additions & 12 deletions test/models/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,46 @@ def setup
assert new_fasta_attachment_ext_fna_gz.fasta?
end

test 'metadata text file types' do
new_text_attachment_ext_txt =
@sample.attachments.build(file: { io: Rails.root.join('test/fixtures/files/test_file_14.txt').open,
filename: 'test_file_14.txt' })
new_text_attachment_ext_txt.save
assert_equal 'text', new_text_attachment_ext_txt.metadata['format']

new_text_attachment_ext_rtf =
@sample.attachments.build(file: { io: Rails.root.join('test/fixtures/files/test_file_13.rtf').open,
filename: 'test_file_13.rtf' })
new_text_attachment_ext_rtf.save
assert_equal 'text', new_text_attachment_ext_rtf.metadata['format']

new_text_attachment_ext_csv =
@sample.attachments.build(file: { io: Rails.root.join('test/fixtures/files/metadata/valid.csv').open,
filename: 'valid.csv' })
new_text_attachment_ext_csv.save
assert_equal 'text', new_text_attachment_ext_csv.metadata['format']

new_text_attachment_ext_tsv =
@sample.attachments.build(file: { io: Rails.root.join('test/fixtures/files/metadata/valid.tsv').open,
filename: 'valid.tsv' })
new_text_attachment_ext_tsv.save
assert_equal 'text', new_text_attachment_ext_tsv.metadata['format']
end

test 'metadata spreadsheet file types' do
new_spreadsheet_attachment_ext_xls =
@sample.attachments.build(file: { io: Rails.root.join('test/fixtures/files/metadata/valid.xls').open,
filename: 'valid.xls' })
new_spreadsheet_attachment_ext_xls.save
assert_equal 'spreadsheet', new_spreadsheet_attachment_ext_xls.metadata['format']

new_spreadsheet_attachment_ext_xlsx =
@sample.attachments.build(file: { io: Rails.root.join('test/fixtures/files/metadata/valid.xlsx').open,
filename: 'valid.xlsx' })
new_spreadsheet_attachment_ext_xlsx.save
assert_equal 'spreadsheet', new_spreadsheet_attachment_ext_xlsx.metadata['format']
end

test 'metadata unknown file types' do
new_unknown_attachment_ext_docx =
@sample.attachments.build(file: { io: Rails.root.join('test/fixtures/files/test_file_11.docx').open,
Expand All @@ -113,18 +153,6 @@ def setup
filename: 'test_file_12.pdf' })
new_unknown_attachment_ext_pdf.save
assert_equal 'unknown', new_unknown_attachment_ext_pdf.metadata['format']

new_unknown_attachment_ext_rtf =
@sample.attachments.build(file: { io: Rails.root.join('test/fixtures/files/test_file_13.rtf').open,
filename: 'test_file_13.rtf' })
new_unknown_attachment_ext_rtf.save
assert_equal 'unknown', new_unknown_attachment_ext_rtf.metadata['format']

new_unknown_attachment_ext_txt =
@sample.attachments.build(file: { io: Rails.root.join('test/fixtures/files/test_file_14.txt').open,
filename: 'test_file_14.txt' })
new_unknown_attachment_ext_txt.save
assert_equal 'unknown', new_unknown_attachment_ext_txt.metadata['format']
end

test '#destroy does not destroy the ActiveStorage::Attachment' do
Expand Down

0 comments on commit 7f64695

Please sign in to comment.