Skip to content

Commit

Permalink
11617: Always include question code at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperka committed Mar 31, 2021
1 parent 271e199 commit 6d847ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
12 changes: 5 additions & 7 deletions app/models/media/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ def dynamic_thumb?

private

# Set a useful filename to assist data analysts who deal with lots of downloads.
# Media not in any group: nemo-response-id_question-code
# Media in regular group: nemo-responseid_question-code
# Media in repeat group: nemo-responseid_repeatgroupname_answer-rank
# Media in nested repeat groups: nemo-responseid_repeatgroupname_answer-rank_repeatgroupname_answer-rank
# Set a useful filename to assist data analysts who deal with lots of downloads, e.g.:
# Media not in any group: nemo-responseId-questionCode
# Media in regular group: nemo-responseId-questionCode
# Media in repeat group(s): nemo-responseId-repeatGroupName(s)-questionCode
def generate_media_object_filename
return if item.record.answer_id.nil?
answer = item.record.answer
Expand All @@ -75,9 +74,8 @@ def build_filename(filename, item)
if answer_group.present? && answer_group.repeatable?
repeat_groups = respect_ancestors(answer_group, repeat_groups)
filename += "-#{repeat_groups.pop}" until repeat_groups.empty?
else
filename += "_#{answer.question.code}"
end
filename += "-#{answer.question.code}"
filename += File.extname(item.filename.to_s)
filename.gsub(/[^0-9A-Za-z.\-]/, "_")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ def build_filename(filename, item)
if answer_group.present? && answer_group.repeatable?
repeat_groups = respect_ancestors(answer_group, repeat_groups)
filename += "-#{repeat_groups.pop}" until repeat_groups.empty?
else
filename += "_#{answer.question.code}"
end
filename += "-#{answer.question.code}"
filename += File.extname(item.filename.to_s)
filename.gsub(/[^0-9A-Za-z.\-]/, "_")
end
Expand Down
13 changes: 8 additions & 5 deletions spec/models/media/object_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

# rubocop:disable Layout/LineLength
# == Schema Information
#
# Table name: media_objects
Expand All @@ -21,6 +23,8 @@
#
# media_objects_answer_id_fkey (answer_id => answers.id) ON DELETE => restrict ON UPDATE => restrict
#
# rubocop:enable Layout/LineLength

require "rails_helper"

# rubocop:disable Layout/MultilineHashBraceLayout
Expand Down Expand Up @@ -77,8 +81,8 @@
filename1 = r.root_node.c[1].c[1].c[1].c[0].c[0].media_object.item.blob.filename.to_s
filename2 = r.root_node.c[1].c[1].c[1].c[1].c[0].media_object.item.blob.filename.to_s

expect(filename1).to match(/Person2-Eyes1.jpg/)
expect(filename2).to match(/Person2-Eyes2.png/)
expect(filename1).to match(/-Person2-Eyes1-.+.jpg/)
expect(filename2).to match(/-Person2-Eyes2-.+.png/)
end
end

Expand Down Expand Up @@ -139,19 +143,18 @@
filename1 = r.root_node.c[0].c[0].c[1].c[0].media_object.item.blob.filename.to_s
group_name = r.root_node.c[0].group_name.gsub(/\s+/, "_").to_s
expect(filename1).to include(group_name)
expect(filename1).to match(/Person2.png/)
expect(filename1).to match(/-Person2-.+.png/)

filename2 = r.root_node.c[0].c[0].c[0].c[0].media_object.item.blob.filename.to_s
expect(filename2).to include(group_name)
expect(filename2).to match(/Person1.jpg/)
expect(filename2).to match(/-Person1-.+.jpg/)
end
end

def create_response(params)
responses << create(:response, params)
end
end

# rubocop:enable Layout/MultilineHashBraceLayout
# rubocop:enable Layout/HashAlignment
# rubocop:enable Style/WordArray

0 comments on commit 6d847ef

Please sign in to comment.