Refactor `AnswersController#create_or_update` by aaronskiba · Pull Request #3519 · DMPRoadmap/roadmap

added 2 commits

April 29, 2025 10:25
- This refactor addresses the Style/GuardClause offense. It also makes the code easier to read by replacing the large `if @answer.present?` block with the early `return unless @answer.present?`
- The code comment has also been updated to correspond with the refactor.
- This refactor simply applies DRY principles to some of the plan fetching code in `AnswersController#create_or_update`.
- Cut and pasted existing answer transaction logic to new `handle_answer_transaction` helper method.
- This refactor is intended to isolate the complex logic for the `Answer.transaction` block and to make the underlying `create_or_update` action easier to read and maintain.
These changes refactor the `Answer.transaction do` block into separate `create_answer`, `update_answer`, and `handle_stale_answer_error` methods.

The changes are almost entirely a direct cut/paste from the original block into the corresponding methods. The only alteration is the prior `if q.question_format.rda_metadata?` check to `return unless q.question_format.rda_metadata?`. This early return check was auto-applied by rubocop.
- This change addresses the following rubocop offences:

```
app/controllers/answers_controller.rb:125:43: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
  def handle_answer_transaction(p_params, q)
                                          ^
app/controllers/answers_controller.rb:144:27: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
  def create_answer(args, q, standards)
                          ^
app/controllers/answers_controller.rb:156:27: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
  def update_answer(args, q, standards)
```
-This change maintains the functional logic of the deletion of answers (based on questions to be removed) while also optimising its efficiency.

@aaronskiba

- This change updates/corrects the commented locals in `app/views/answers/_new_edit.html.erb`.
- Prior to this change, there were conflicting comments listing the available locals. `plan` does not appear to be one of them. Also, `app/controllers/answers_controller.rb` reveals that `base_template_org` is one of them.

@aaronskiba aaronskiba changed the title Aaron/conditional question fix for removing questions bug rails7 Refactor AnswersController#create_or_update

Apr 29, 2025

@aaronskiba