Skip to content

Commit 58ec2c9

Browse files
committed
Change questions_with_answer_type page download CSV link to go to self
Rather than going to the reports#live_csv_questions action for the CSV of questions with answer type, we should use the same pattern as for the other feature reports and use the same URL but with a `.csv` appended for the CSV download link on the questions with answer type report page. This commit updates the #questions_with_answer_type action to accept format `csv` as well as `html`, same as the other feature report actions, and uses that in the view template. We can then also simplify the live_questions_csv action. This probably should have been done in PR #1976, but it was missed.
1 parent db1c43e commit 58ec2c9

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

app/controllers/reports_controller.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ def questions_with_answer_type
1616
forms = Reports::FormDocumentsService.live_form_documents
1717
questions = Reports::FeatureReportService.new(forms).questions_with_answer_type(answer_type)
1818

19-
render template: "reports/questions_with_answer_type", locals: { answer_type:, questions: }
19+
if params[:format] == "csv"
20+
send_data Reports::QuestionsCsvReportService.new(questions).csv,
21+
type: "text/csv; charset=iso-8859-1",
22+
disposition: "attachment; filename=#{questions_csv_filename(answer_type)}"
23+
else
24+
render template: "reports/questions_with_answer_type", locals: { answer_type:, questions: }
25+
end
2026
end
2127

2228
def questions_with_add_another_answer
@@ -120,17 +126,12 @@ def live_forms_csv
120126
end
121127

122128
def live_questions_csv
123-
answer_type = params[:answer_type]
124129
forms = Reports::FormDocumentsService.live_form_documents
125-
questions = if answer_type
126-
Reports::FeatureReportService.new(forms).questions_with_answer_type(answer_type)
127-
else
128-
Reports::FeatureReportService.new(forms).questions
129-
end
130+
questions = Reports::FeatureReportService.new(forms).questions
130131

131132
send_data Reports::QuestionsCsvReportService.new(questions).csv,
132133
type: "text/csv; charset=iso-8859-1",
133-
disposition: "attachment; filename=#{questions_csv_filename(answer_type)}"
134+
disposition: "attachment; filename=#{csv_filename('live_questions_report')}"
134135
end
135136

136137
private

app/views/reports/questions_with_answer_type.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="govuk-grid-column-two-thirds">
55
<h1 class="govuk-heading-l"><%= t(".heading", answer_type: t("helpers.label.page.answer_type_options.names.#{answer_type}").downcase) %></h1>
66

7-
<p><%=govuk_link_to(t(".download_csv"), report_live_questions_csv_path(answer_type:))%></p>
7+
<p><%=govuk_link_to(t(".download_csv"), url_for(format: :csv))%></p>
88
</div>
99

1010
<div class="govuk-grid-column-full">

spec/requests/reports_controller_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,28 @@
828828
end
829829
end
830830

831+
describe "#questions_with_answer_type as csv" do
832+
before do
833+
login_as_super_admin_user
834+
835+
travel_to Time.utc(2025, 5, 15, 15, 31, 57)
836+
837+
get report_questions_with_answer_type_path(answer_type: "text", format: :csv)
838+
end
839+
840+
it_behaves_like "csv response"
841+
842+
it "responds with an attachment content-disposition header" do
843+
expect(response.headers["content-disposition"]).to match("attachment; filename=live_questions_report_text_answer_type-2025-05-15 15:31:57 UTC.csv")
844+
end
845+
846+
it "has expected response body" do
847+
csv = CSV.parse(response.body, headers: true)
848+
expect(csv.headers).to eq Reports::QuestionsCsvReportService::QUESTIONS_CSV_HEADERS
849+
expect(csv.length).to eq 5
850+
end
851+
end
852+
831853
describe "#questions_with_add_another_answer as csv" do
832854
before do
833855
login_as_super_admin_user

spec/views/reports/questions_with_answer_type.html.erb_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
]
99
end
1010

11+
let(:answer_type) { "email" }
12+
1113
before do
12-
render locals: { answer_type: "email", questions: }
14+
controller.request.path_parameters[:answer_type] = answer_type
15+
16+
render locals: { answer_type:, questions: }
1317
end
1418

1519
describe "page title" do
@@ -23,7 +27,7 @@
2327
end
2428

2529
it "has a link to download the CSV" do
26-
expect(rendered).to have_link("Download all questions in live forms with this answer type as a CSV file", href: report_live_questions_csv_path(answer_type: "email"))
30+
expect(rendered).to have_link("Download all questions in live forms with this answer type as a CSV file", href: report_questions_with_answer_type_path(answer_type: "email", format: :csv))
2731
end
2832

2933
describe "questions table" do

0 commit comments

Comments
 (0)