Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/forms/welsh_translation_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def welsh_translation_input_params
end

def page_translation_input_params
params.require(:forms_welsh_translation_input).permit(page_translations: WelshPageTranslationInput.attribute_names)
params.require(:forms_welsh_translation_input).permit(page_translations: [{ selection_options_cy: {} }, *WelshPageTranslationInput.attribute_names])
end

def welsh_enabled?
Expand Down
43 changes: 43 additions & 0 deletions app/input_objects/forms/welsh_page_translation_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Forms::WelshPageTranslationInput < BaseInput
attribute :hint_text_cy
attribute :page_heading_cy
attribute :guidance_markdown_cy
attribute :selection_options_cy, default: []

def submit
return false if invalid?
Expand All @@ -17,6 +18,12 @@ def submit
page.page_heading_cy = page_has_page_heading_and_guidance_markdown? ? page_heading_cy : nil
page.guidance_markdown_cy = page_has_page_heading_and_guidance_markdown? ? guidance_markdown_cy : nil

if page_has_selection_options?
welsh_answer_settings = page.answer_settings.dup
welsh_answer_settings.selection_options = selection_options_cy
page.answer_settings_cy = welsh_answer_settings
end

page.save!
end

Expand All @@ -26,6 +33,29 @@ def assign_page_values
self.page_heading_cy = page.page_heading_cy
self.guidance_markdown_cy = page.guidance_markdown_cy

# If our welsh translations don't have answer_settings, we need to copy
# across the english ones and reset any text
if page.answer_settings && page.answer_settings_cy.blank?
# Use as_json to get a hash. It's a possibly nested DataStruct and
# deep_dup or dup doesn't work
answer_settings_cloned = page.answer_settings.as_json

# Reset the selection_options names if it's a selection type
if page.answer_settings.selection_options.present?
# Clear the selection_options names, as we don't have any welsh translations yet
answer_settings_cloned["selection_options"].each do |option|
option["name"] = nil
end

# Save to the welsh answer_settings, where if will become a DataStruct
page.answer_settings_cy = answer_settings_cloned
end
end

if page_has_selection_options?
self.selection_options_cy = page.answer_settings_cy&.selection_options
end

self
end

Expand All @@ -34,6 +64,15 @@ def page
@page
end

def selection_options_cy=(incoming_hash)
if incoming_hash.is_a?(Hash)
values_array = incoming_hash.values
super(values_array)
else
super(incoming_hash)
end
end

def form_field_id(attribute)
field_id(:forms_welsh_page_translation_input, page.id, :page_translations, attribute)
end
Expand All @@ -45,4 +84,8 @@ def page_has_hint_text?
def page_has_page_heading_and_guidance_markdown?
page.page_heading.present? && page.guidance_markdown.present?
end

def page_has_selection_options?
page.answer_type == "selection"
end
end
3 changes: 3 additions & 0 deletions app/input_objects/forms/welsh_translation_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def submit
end

form.welsh_completed = mark_complete

form.available_languages = %i[en cy]

form.save_draft!
end

Expand Down
19 changes: 18 additions & 1 deletion app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,28 @@ def as_form_document_step(next_page)
"position" => position,
"next_step_id" => next_page&.id,
"type" => "question_page",
"data" => slice(*%w[question_text hint_text answer_type is_optional answer_settings page_heading guidance_markdown is_repeatable]),
"data" => slice(*%w[question_text hint_text answer_type is_optional page_heading guidance_markdown is_repeatable]).merge(answer_settings: answer_settings_with_modified_selection_options),
"routing_conditions" => routing_conditions.map(&:as_form_document_condition),
}
end

def answer_settings_with_modified_selection_options
return answer_settings if answer_settings&.selection_options.blank?

answer_settings_with_values = answer_settings_en.selection_options.map.with_index do |option, index|
{
"value" => option[:name],
# Set name to value from current locale answer_settings.
# If we don't have a value, use the english name as the default
"name" => answer_settings.selection_options[index][:name].presence || option[:name],
}
end

answer_settings.dup.tap do |answer_settings|
answer_settings["selection_options"] = DataStructType.new.cast_value(answer_settings_with_values)
end
end

def secondary_skip_condition
check_conditions.where(answer_value: nil).where.not("check_page_id = routing_page_id").first
end
Expand Down
35 changes: 26 additions & 9 deletions app/services/form_document_sync_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,42 @@ def synchronize_form(form)
end

def update_draft_form_document(form)
update_or_create_form_document(form, "draft", form.as_form_document)
form.available_languages.each do |language|
Mobility.with_locale(language) do
content = form.as_form_document
update_or_create_form_document(form, "draft", content, language:)
end
end
end

private

def sync_live_form(form)
content = form.as_form_document(live_at: form.updated_at)
update_or_create_form_document(form, "live", content)
delete_form_documents(form, "archived")
FormDocument.transaction do
form.available_languages.each do |language|
Mobility.with_locale(language) do
content = form.as_form_document(live_at: form.updated_at)
update_or_create_form_document(form, "live", content, language:)
delete_form_documents(form, "archived")
end
end
end
end

def sync_archived_form(form)
live_form_document = FormDocument.find_by!(form:, tag: "live")
update_or_create_form_document(form, "archived", live_form_document.content)
delete_form_documents(form, "live")
FormDocument.transaction do
form.available_languages.each do |language|
Mobility.with_locale(language) do
live_form_document = FormDocument.find_by!(form:, tag: "live")
update_or_create_form_document(form, "archived", live_form_document.content, language:)
delete_form_documents(form, "live")
end
end
end
end

def update_or_create_form_document(form, tag, content)
form_document = FormDocument.find_or_initialize_by(form_id: form.id, tag:)
def update_or_create_form_document(form, tag, content, language: "en")
form_document = FormDocument.find_or_initialize_by(form_id: form.id, tag:, language:)
form_document.content = content
form_document.save!
end
Expand Down
21 changes: 21 additions & 0 deletions app/views/forms/welsh_translation/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,27 @@
<% end %>
<% end %>
<% end %>
<% if page_translation.page_has_selection_options? %>
<% page_translation.page.answer_settings.selection_options.each_with_index do |option, index| %>
<%= body.with_row do |row| %>
<%= row.with_cell %>
<%= row.with_cell(text: option.name) %>
<%= page_translation_fields.govuk_fieldset legend: { text: t("selection_options.add_options") } do %>
<%= row.with_cell do %>
<%= page_translation_fields.fields_for :selection_options_cy, page_translation.page.answer_settings_cy.selection_options[index], index: index do |selection_options_form| %>
<%= selection_options_form.govuk_text_field :name, id: "forms-selections-settings-form-selection-options-name-field-#{index}", label: {
text: t("selection_options.option", option_number: index + 1),
class: "govuk-visually-hidden",
for: "forms-selections-settings-form-selection-options-name-field-#{index}",
},
class: "govuk-input--width-20",
form_group: { classes: "app-select-options__form-group" } %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions spec/services/reports/questions_csv_report_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"true",
"2",
nil,
"{\"only_one_option\" => \"true\", \"selection_options\" => [{\"name\" => \"Option 1\"}, {\"name\" => \"Option 2\"}]}",
"{\"only_one_option\" => \"true\", \"selection_options\" => [{\"name\" => \"Option 1\", \"value\" => \"Option 1\"}, {\"name\" => \"Option 2\", \"value\" => \"Option 2\"}]}",
)
end

Expand Down Expand Up @@ -175,7 +175,7 @@
"true",
"2",
nil,
"{\"only_one_option\" => \"true\", \"selection_options\" => [{\"name\" => \"Option 1\"}, {\"name\" => \"Option 2\"}]}",
"{\"only_one_option\" => \"true\", \"selection_options\" => [{\"name\" => \"Option 1\", \"value\" => \"Option 1\"}, {\"name\" => \"Option 2\", \"value\" => \"Option 2\"}]}",
)
end

Expand Down Expand Up @@ -205,7 +205,7 @@
"true",
"2",
nil,
"{\"only_one_option\" => \"true\", \"selection_options\" => [{\"name\" => \"Option 1\"}, {\"name\" => \"Option 2\"}]}",
"{\"only_one_option\" => \"true\", \"selection_options\" => [{\"name\" => \"Option 1\", \"value\" => \"Option 1\"}, {\"name\" => \"Option 2\", \"value\" => \"Option 2\"}]}",
)
end
end
Expand Down