From 426fee52857655217b9019e6cc62ee6feb7c584c Mon Sep 17 00:00:00 2001 From: Kevin Cooper Date: Thu, 14 Mar 2024 18:38:00 -0400 Subject: [PATCH] 12527: use null-safe array access for translations e.g. q.question.hint_translations can be nil --- app/models/forms/export.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/app/models/forms/export.rb b/app/models/forms/export.rb index 6f01b19908..f588458624 100644 --- a/app/models/forms/export.rb +++ b/app/models/forms/export.rb @@ -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 @@ -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, @@ -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) @@ -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) @@ -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 @@ -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)