Skip to content

Conversation

@lfdebrux
Copy link
Member

@lfdebrux lfdebrux commented May 22, 2025

What problem does this pull request solve?

I wanted to add a new feature report, but I found the feature report code was very repetitive and I had to add a lot of files to do that, and I felt like this situation could be improved. So in this PR, I try to make the feature report code more DRY.

A big part of this is making the responsibilities for each class involved in making a feature report more clearly deliniated and less overlapping, with the controller creating the pipeline to get the final result. It is now expected that FormDocumentsService will be called first, and that passed to FeatureReportService with is responsible just for filtering form documents and optionally converting it into a list of questions. There are helpers that turn this array of form documents or question page documents into a GOV.UK styles table; if a CSV is wanted for the end result, FormsCsvReportService generates the CSV for an array of form documents and QuestionsCsvReportService generates the CSV for an array of question page documents.

This could be taken further still, to the point where adding a new report requires only adding a new method to FeatureReportService; this PR was split out from #1970.

Things to consider when reviewing

  • Ensure that you consider the wider context.
  • Does it work when run on your machine?
  • Is it clear what the code is doing?
  • Do the commit messages explain why the changes were made?
  • Are there all the unit tests needed?
  • Do the end to end tests need updating before these changes will pass?
  • Has all relevant documentation been updated?

@lfdebrux lfdebrux changed the title Refactor feature reports Refactor feature reports (part 1) May 22, 2025
@lfdebrux lfdebrux force-pushed the ldeb-refactor-feature-reports--part-1 branch from 0c74dc8 to 7e02085 Compare May 22, 2025 15:51
Copy link
Contributor

@stephencdaly stephencdaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice improvements that are logically broken down.

I've compared the output of the reports and the CSV downloads with some local data and they look the same after these changes from what I can see.

Just the minor issue around the table formatting when an organisation isn't set for a form that we talked about on slack, but otherwise it's good to go.

@lfdebrux
Copy link
Member Author

Just the minor issue around the table formatting when an organisation isn't set for a form that we talked about on slack, but otherwise it's good to go.

Fixed now in Fix organisation name when form is not in group.

@lfdebrux lfdebrux closed this May 23, 2025
@lfdebrux lfdebrux reopened this May 23, 2025
@lfdebrux lfdebrux changed the title Refactor feature reports (part 1) Refactor feature reports to make it easier to add new feature reports May 23, 2025
@lfdebrux lfdebrux force-pushed the ldeb-refactor-feature-reports--part-1 branch from d9c0261 to 6e4152c Compare May 23, 2025 09:12
lfdebrux added 15 commits May 23, 2025 12:28
- Add form with routing to fixture
- Add more request specs for report CSV downloads
- Refactor view specs to remove explicit template
- Make it clear what method is being described in Reports::FormDocumentsService specs
Pass head and rows to govuk_table [[1]] as arrays of strings, rather than
constructing the table in each view template.

This reduces repetition and makes reuse easier.

[1]: https://govuk-components.netlify.app/components/table
Remove references to `live` from the report key names, so the code is
more generic and reusable.
Rather than call FormDocumentsService inside FeatureReportService and
couple those two classes together, change FeatureReportService to expect
to be passed a list of form_documents. This also allows in future for
the source of form documents (for instance, from live to draft) to be
changed without needing to change FeatureReportService.
- Scope routes for feature reports
- Make feature report CSV paths more similar to corresponding HTML page paths
- Add routing specs

Change the paths for routes related to feature reports to be more
consistent and neatly scoped together. The purpose of this is to
highlight similarities and eventually be more DRY.
Rather than call FormDocumentsService inside CsvReportsService and
couple those two classes together, change CsvReportService to expect to
be passed a list of form_documents. This allows us to reuse the
filtering logic in FeatureReportService instead of reimplementing it in
CsvReportsService, lets us change the source of documents in future, and
also lets us increases the commonality in the controller actions for
rendering a report in HTML and CSV so they might be combined.
Put code for generating CSV of questions into QuestionCsvReportService
and code for generating CSV of forms into FormCsvReportService.
Rather than have QuestionCsvReportService extract and filter questions
from forms itself, make it expect to be passed a list of question_page
documents, so that we can reuse the code in FeatureReportService and
reduce duplication.
Rather than having two different controller actions for the same data in
two different formats, we can (now that we have made the code similar
enough) combine the pairs into one each, differentiating by the format
suffix in the request path [[1]].

The main advantage of this is that when adding a new feature report in
future there is less to do.

[1]: https://guides.rubyonrails.org/routing.html#format-segments
Change the template for each feature report to get the translation keys
using a local, rather than the conventional Rails shorthand. The
advantage of this is that the templates all start to look the same...
Now that the templates all look the same for routes for feature reports,
we can use the same one template for all routes. This means a lot less
work needed for adding new feature reports in future.
@lfdebrux lfdebrux force-pushed the ldeb-refactor-feature-reports--part-1 branch from 6e4152c to fdbd38e Compare May 23, 2025 09:28
@sonarqubecloud
Copy link

@github-actions
Copy link

🎉 A review copy of this PR has been deployed! You can reach it at: https://pr-1976.admin.review.forms.service.gov.uk/

It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready
after 5 minutes, there may be something wrong with the ECS task. You will need to go to the integration AWS account
to debug, or otherwise ask an infrastructure person.

For the sign in details and more information, see the review apps wiki page.

@lfdebrux lfdebrux merged commit fbd64e7 into main May 23, 2025
6 checks passed
@lfdebrux lfdebrux deleted the ldeb-refactor-feature-reports--part-1 branch May 23, 2025 09:37
lfdebrux added a commit that referenced this pull request May 29, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants