Skip to content

Commit

Permalink
12527: use null-safe array access for translations
Browse files Browse the repository at this point in the history
e.g. q.question.hint_translations can be nil
  • Loading branch information
cooperka committed Mar 15, 2024
1 parent 65947ff commit 426fee5
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions app/models/forms/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def to_xls

# write translated label and hint columns
locales.each do |locale|
questions.row(row_index).push(q.group_name_translations[locale.to_s],
q.group_hint_translations[locale.to_s])
questions.row(row_index).push(q.group_name_translations&.dig(locale.to_s),
q.group_hint_translations&.dig(locale.to_s))
end

# write group name
Expand Down Expand Up @@ -182,8 +182,8 @@ def to_xls

# write translated label and hint columns
locales.each do |locale|
questions.row(row_index + l_index).push(q.question.name_translations[locale.to_s],
q.question.hint_translations[locale.to_s])
questions.row(row_index + l_index).push(q.question.name_translations&.dig(locale.to_s),
q.question.hint_translations&.dig(locale.to_s))
end

questions.row(row_index + l_index).push(name_to_push,
Expand All @@ -203,8 +203,8 @@ def to_xls
questions.row(row_index).push(type_to_push)
# write translated label and hint columns
locales.each do |locale|
questions.row(row_index).push(q.question.name_translations[locale.to_s],
q.question.hint_translations[locale.to_s])
questions.row(row_index).push(q.question.name_translations&.dig(locale.to_s),
q.question.hint_translations&.dig(locale.to_s))
end
questions.row(row_index).push(q.code, q.required.to_s,
conditions_to_push, constraints_to_push, choice_filter)
Expand All @@ -214,8 +214,8 @@ def to_xls
questions.row(row_index).push(qtype_converted)
# write translated label and hint columns
locales.each do |locale|
questions.row(row_index).push(q.question.name_translations[locale.to_s],
q.question.hint_translations[locale.to_s])
questions.row(row_index).push(q.question.name_translations&.dig(locale.to_s),
q.question.hint_translations&.dig(locale.to_s))
end
questions.row(row_index).push(q.code, q.required.to_s,
conditions_to_push, constraints_to_push, choice_filter)
Expand Down Expand Up @@ -402,7 +402,7 @@ def options_to_xls(option_sets, locales)

# push translated label columns
locales.each do |locale|
option_row.push(node.option.name_translations[locale.to_s])
option_row.push(node.option.name_translations&.dig(locale.to_s))
end

option_row += level_to_push # append levels, if any, to rightmost columns
Expand All @@ -429,9 +429,11 @@ def options_to_xls(option_sets, locales)
os_matrix.insert(0, header_row)
end

# recursively remove pesky characters and replace spaces with underscores
# for XLSForm compatibility
def vanillify(input)
# remove extra characters and replace spaces with underscores
# for XLSForm compatibility
return "" if input.nil?

if input.instance_of?(String)
input.vanilla.tr(" ", "_")
elsif input.instance_of?(Array)
Expand Down

0 comments on commit 426fee5

Please sign in to comment.