Skip to content

Commit fbd64e7

Browse files
authored
Merge pull request #1976 from alphagov/ldeb-refactor-feature-reports--part-1
Refactor feature reports to make it easier to add new feature reports
2 parents 6819417 + fdbd38e commit fbd64e7

31 files changed

Lines changed: 1748 additions & 1151 deletions

app/controllers/reports_controller.rb

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,70 @@ class ReportsController < ApplicationController
55
def index; end
66

77
def features
8-
data = Reports::FeatureReportService.report
8+
forms = Reports::FormDocumentsService.live_form_documents
9+
data = Reports::FeatureReportService.new(forms).report
910

1011
render template: "reports/features", locals: { data: }
1112
end
1213

1314
def questions_with_answer_type
1415
answer_type = params.require(:answer_type)
15-
questions = Reports::FeatureReportService.questions_with_answer_type(answer_type)
16+
forms = Reports::FormDocumentsService.live_form_documents
17+
questions = Reports::FeatureReportService.new(forms).questions_with_answer_type(answer_type)
1618

1719
render template: "reports/questions_with_answer_type", locals: { answer_type:, questions: }
1820
end
1921

2022
def questions_with_add_another_answer
21-
questions = Reports::FeatureReportService.live_questions_with_add_another_answer
23+
forms = Reports::FormDocumentsService.live_form_documents
24+
questions = Reports::FeatureReportService.new(forms).questions_with_add_another_answer
2225

23-
render template: "reports/questions_with_add_another_answer", locals: { questions: }
26+
if params[:format] == "csv"
27+
send_data Reports::QuestionsCsvReportService.new(questions).csv,
28+
type: "text/csv; charset=iso-8859-1",
29+
disposition: "attachment; filename=#{csv_filename('live_questions_with_add_another_answer_report')}"
30+
else
31+
render template: "reports/feature_report", locals: { report: params[:action], records: questions }
32+
end
2433
end
2534

2635
def forms_with_routes
27-
forms = Reports::FeatureReportService.live_forms_with_routes
36+
forms = Reports::FormDocumentsService.live_form_documents
37+
forms = Reports::FeatureReportService.new(forms).forms_with_routes
2838

29-
render template: "reports/forms_with_routes", locals: { forms: forms }
39+
if params[:format] == "csv"
40+
send_data Reports::FormsCsvReportService.new(forms).csv,
41+
type: "text/csv; charset=iso-8859-1",
42+
disposition: "attachment; filename=#{csv_filename('live_forms_with_routes_report')}"
43+
else
44+
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
45+
end
3046
end
3147

3248
def forms_with_payments
33-
forms = Reports::FeatureReportService.live_forms_with_payments
49+
forms = Reports::FormDocumentsService.live_form_documents
50+
forms = Reports::FeatureReportService.new(forms).forms_with_payments
3451

35-
render template: "reports/forms_with_payments", locals: { forms: forms }
52+
if params[:format] == "csv"
53+
send_data Reports::FormsCsvReportService.new(forms).csv,
54+
type: "text/csv; charset=iso-8859-1",
55+
disposition: "attachment; filename=#{csv_filename('live_forms_with_payments_report')}"
56+
else
57+
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
58+
end
3659
end
3760

3861
def forms_with_csv_submission_enabled
39-
forms = Reports::FeatureReportService.live_forms_with_csv_submission_enabled
62+
forms = Reports::FormDocumentsService.live_form_documents
63+
forms = Reports::FeatureReportService.new(forms).forms_with_csv_submission_enabled
4064

41-
render template: "reports/forms_with_csv_submission_enabled", locals: { forms: forms }
65+
if params[:format] == "csv"
66+
send_data Reports::FormsCsvReportService.new(forms).csv,
67+
type: "text/csv; charset=iso-8859-1",
68+
disposition: "attachment; filename=#{csv_filename('live_forms_with_csv_submission_enabled_report')}"
69+
else
70+
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
71+
end
4272
end
4373

4474
def users
@@ -82,42 +112,27 @@ def selection_questions_with_checkboxes
82112
def csv_downloads; end
83113

84114
def live_forms_csv
85-
send_data Reports::CsvReportsService.new.live_forms_csv,
86-
type: "text/csv; charset=iso-8859-1",
87-
disposition: "attachment; filename=#{csv_filename('live_forms_report')}"
88-
end
115+
forms = Reports::FormDocumentsService.live_form_documents
89116

90-
def live_forms_with_routes_csv
91-
send_data Reports::CsvReportsService.new.live_forms_with_routes_csv,
117+
send_data Reports::FormsCsvReportService.new(forms).csv,
92118
type: "text/csv; charset=iso-8859-1",
93-
disposition: "attachment; filename=#{csv_filename('live_forms_with_routes_report')}"
94-
end
95-
96-
def live_forms_with_payments_csv
97-
send_data Reports::CsvReportsService.new.live_forms_with_payments_csv,
98-
type: "text/csv; charset=iso-8859-1",
99-
disposition: "attachment; filename=#{csv_filename('live_forms_with_payments_report')}"
100-
end
101-
102-
def live_forms_with_csv_submission_enabled_csv
103-
send_data Reports::CsvReportsService.new.live_forms_with_csv_submission_enabled_csv,
104-
type: "text/csv; charset=iso-8859-1",
105-
disposition: "attachment; filename=#{csv_filename('live_forms_with_csv_submission_enabled_report')}"
119+
disposition: "attachment; filename=#{csv_filename('live_forms_report')}"
106120
end
107121

108122
def live_questions_csv
109123
answer_type = params[:answer_type]
110-
send_data Reports::CsvReportsService.new.live_questions_csv(answer_type:),
124+
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+
131+
send_data Reports::QuestionsCsvReportService.new(questions).csv,
111132
type: "text/csv; charset=iso-8859-1",
112133
disposition: "attachment; filename=#{questions_csv_filename(answer_type)}"
113134
end
114135

115-
def live_questions_with_add_another_answer_csv
116-
send_data Reports::CsvReportsService.new.live_questions_with_add_another_answer_csv,
117-
type: "text/csv; charset=iso-8859-1",
118-
disposition: "attachment; filename=#{csv_filename('live_questions_with_add_another_answer_report')}"
119-
end
120-
121136
private
122137

123138
def check_user_has_permission

app/helpers/report_helper.rb

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
module ReportHelper
2+
def report_table(records)
3+
type = records.first.fetch("type", "form")
4+
5+
if type == "form"
6+
with_routes = records.first.dig("metadata", "number_of_routes").present?
7+
8+
with_routes ? report_forms_with_routes_table(records) : report_forms_table(records)
9+
elsif type == "question_page"
10+
report_questions_table(records)
11+
else
12+
raise "type of records '#{type}' is not one of 'forms', 'question_page'"
13+
end
14+
end
15+
16+
def report_forms_table(forms)
17+
{
18+
head: report_forms_table_head,
19+
rows: report_forms_table_rows(forms),
20+
}
21+
end
22+
23+
def report_forms_with_routes_table(forms)
24+
{
25+
head: report_forms_with_routes_table_head,
26+
rows: report_forms_with_routes_table_rows(forms),
27+
}
28+
end
29+
30+
def report_questions_table(questions)
31+
{
32+
head: report_questions_table_head,
33+
rows: report_questions_table_rows(questions),
34+
}
35+
end
36+
37+
def report_forms_table_head
38+
[
39+
I18n.t("reports.form_or_questions_list_table.headings.form_name"),
40+
I18n.t("reports.form_or_questions_list_table.headings.organisation"),
41+
]
42+
end
43+
44+
def report_forms_table_rows(forms)
45+
forms.map { |form| report_forms_table_row(form) }
46+
end
47+
48+
def report_forms_with_routes_table_head
49+
[
50+
*report_forms_table_head,
51+
I18n.t("reports.form_or_questions_list_table.headings.number_of_routes"),
52+
]
53+
end
54+
55+
def report_forms_with_routes_table_rows(forms)
56+
forms.map { |form| report_forms_with_routes_table_row(form) }
57+
end
58+
59+
def report_questions_table_head
60+
[
61+
*report_forms_table_head,
62+
I18n.t("reports.form_or_questions_list_table.headings.question_text"),
63+
]
64+
end
65+
66+
def report_questions_table_rows(questions)
67+
questions.map { |question| report_questions_table_row(question) }
68+
end
69+
70+
private
71+
72+
def report_forms_table_row(form)
73+
[
74+
govuk_link_to(form["content"]["name"], live_form_pages_path(form_id: form["form_id"])),
75+
form.dig("group", "organisation", "name") || "",
76+
]
77+
end
78+
79+
def report_forms_with_routes_table_row(form)
80+
[
81+
*report_forms_table_row(form),
82+
form["metadata"]["number_of_routes"].to_s,
83+
]
84+
end
85+
86+
def report_questions_table_row(question)
87+
[
88+
*report_forms_table_row(question["form"]),
89+
question["data"]["question_text"],
90+
]
91+
end
92+
end

0 commit comments

Comments
 (0)