Skip to content
Open
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
10 changes: 10 additions & 0 deletions app/controllers/forms/welsh_translation_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ def render_preview
render json: { preview_html:, errors: [] }.to_json
end

def download
authorize current_form, :can_edit_form?

form_content_service = WelshCsvService.new(form_with_pages_and_conditions)

send_data form_content_service.as_csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{form_content_service.filename}"
end

private

def preview_html
Expand Down
126 changes: 126 additions & 0 deletions app/services/welsh_csv_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
class WelshCsvService
MAX_FILENAME_LENGTH = 80
FILENAME_SEPARATOR = "_".freeze

attr_reader :form

def initialize(form)
@form = form
end

def as_csv
CSV.generate do |csv|
add_header(csv)
add_form_name(csv)
add_page_content(csv)
add_form_metadata(csv)
end
end

def filename
extension = ".csv"
safe_form_name = form.name
.parameterize(separator: FILENAME_SEPARATOR)
.truncate(MAX_FILENAME_LENGTH - extension.length, separator: FILENAME_SEPARATOR, omission: "")

"#{safe_form_name}#{extension}"
end

private

def add_header(csv)
csv << ["", "English content", "Welsh content"]
end

def add_form_name(csv)
csv << ["Form name", form.name, form.name_cy]
end

def add_page_content(csv)
form.pages.each do |page|
add_question_content(csv, page)
add_selection_options(csv, page) if page.answer_type == "selection"
add_none_of_above_question(csv, page) if has_none_of_the_above?(page)
add_routing_conditions(csv, page)
add_page_heading(csv, page)
add_guidance_text(csv, page)
end
end

def add_question_content(csv, page)
csv << ["#{question_name(page)} - question text", page.question_text, page.question_text_cy]
if page.hint_text.present?
csv << ["#{question_name(page)} - hint text", page.hint_text, page.hint_text_cy]
end
end

def add_selection_options(csv, page)
page.answer_settings.selection_options.each_with_index do |option, index|
welsh_option_name = page.answer_settings_cy&.selection_options&.dig(index)&.name || ""
csv << ["#{question_name(page)} - option #{index + 1}", option.name, welsh_option_name]
end
end

def add_none_of_above_question(csv, page)
english_question = page.answer_settings.none_of_the_above_question.question_text
welsh_question = page.answer_settings_cy&.none_of_the_above_question&.question_text || ""

csv << [
"#{question_name(page)} - question or label if ‘None of the above’ is selected",
english_question,
welsh_question,
]
end

def has_none_of_the_above?(page)
page.answer_type == "selection" &&
page.answer_settings.none_of_the_above_question.present?
end

def add_routing_conditions(csv, page)
page.routing_conditions.each do |condition|
if condition.is_exit_page?
csv << ["#{question_name(page)} - exit page heading", condition.exit_page_heading, condition.exit_page_heading_cy]
csv << ["#{question_name(page)} - exit page content", condition.exit_page_markdown, condition.exit_page_markdown_cy]
end
end
end

def add_page_heading(csv, page)
if page.page_heading.present?
csv << ["#{question_name(page)} - page heading", page.page_heading, page.page_heading_cy]
end
end

def add_guidance_text(csv, page)
if page.guidance_markdown.present?
csv << ["#{question_name(page)} - guidance text", page.guidance_markdown, page.guidance_markdown_cy]
end
end

def question_name(page)
"Question #{page.position}"
end

def add_form_metadata(csv)
add_field_if_present(csv, "Declaration", form.declaration_text, form.declaration_text_cy)
add_field_if_present(csv, "Information about what happens next", form.what_happens_next_markdown, form.what_happens_next_markdown_cy)
add_field_if_present(csv, "GOV⁠.⁠UK Pay payment link", form.payment_url, form.payment_url_cy)
add_field_if_present(csv, "Link to privacy information for this form", form.privacy_policy_url, form.privacy_policy_url_cy)

add_support_details(csv)
end

def add_support_details(csv)
add_field_if_present(csv, "Contact details for support - email address", form.support_email, form.support_email_cy)
add_field_if_present(csv, "Contact details for support - phone number and opening times", form.support_phone, form.support_phone_cy)
add_field_if_present(csv, "Contact details for support - online contact link", form.support_url, form.support_url_cy)
add_field_if_present(csv, "Contact details for support - online contact link text", form.support_url_text, form.support_url_text_cy)
end

def add_field_if_present(csv, label, english_value, welsh_value)
if english_value.present?
csv << [label, english_value, welsh_value || ""]
end
end
end
5 changes: 3 additions & 2 deletions app/views/forms/welsh_translation/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
<%= t("page_titles.welsh_translation") %>
</h1>

<p>
<div class="govuk-button-group">
<%= govuk_button_link_to t(".csv_download"), welsh_translation_download_path(@welsh_translation_input.form), secondary: true %>
<%= render PreviewLinkComponent::View.new(@welsh_translation_input.form.pages, preview_link(@welsh_translation_input.form, locale: :cy), t(".preview_link_text")) %>
</p>
</div>
</div>
</div>

Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ en:
new:
condition:
heading: Question %{question_number}’s exit page
csv_download: Download content as CSV
delete_welsh_version: Delete Welsh version
english_header: English content
exit_page_heading: Exit page heading
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
get "/welsh-translation/delete" => "forms/welsh_translation#delete", as: :welsh_translation_delete
delete "/welsh-translation/delete" => "forms/welsh_translation#destroy", as: :welsh_translation_destroy
post "/welsh-translation-preview" => "forms/welsh_translation#render_preview", as: :welsh_translation_render_preview
get "/welsh-translation-download" => "forms/welsh_translation#download", as: :welsh_translation_download
get "/submission-attachments" => "forms/submission_attachments#new", as: :submission_attachments
post "/submission-attachments" => "forms/submission_attachments#create", as: :submission_attachments_create
get "/daily-submission-csv" => "forms/daily_submission_batch#new", as: :daily_submission_batch
Expand Down
29 changes: 29 additions & 0 deletions spec/requests/forms/welsh_translation_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,33 @@
end
end
end

describe "#download" do
let(:form) { create(:form, :with_welsh_translation, name: "A form with Welsh") }

before do
get welsh_translation_download_path(id)
end

context "when the user is not authorized" do
let(:current_user) { build :user }

it "returns 403" do
expect(response).to have_http_status(:forbidden)
end
end

it "downloads a CSV file" do
expect(response).to have_http_status(:ok)
expect(response.headers["content-type"]).to eq "text/csv; charset=iso-8859-1"
expect(response.headers["content-disposition"]).to match("attachment; filename=a_form_with_welsh.csv")
end

it "returns a CSV with a header row and and content" do
csv = CSV.parse(response.body)

expect(csv.first).to eq(["", "English content", "Welsh content"])
expect(csv.second).to eq(["Form name", "A form with Welsh", "Welsh A form with Welsh"])
end
end
end
Loading
Loading