Refactor AnswersController#create_or_update#3519
Open
aaronskiba wants to merge 10 commits intoconditional-question-fix-for-removing-questions-bug-rails7from
Open
Conversation
- 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.
Generated by 🚫 Danger |
- 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.
- 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.
436df3a to
b025dfe
Compare
AnswersController#create_or_update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes proposed in this PR:
AnswerController#create_or_updatecreate_or_updateaction into separate private helper methods (commits 609d5df, efecb33, and a840abb).destroy_allrather than iterating through each individual answer.app/javascript/src/utils/sectionUpdate.jslocalsvariables available withinapp/views/answers/_new_edit.html.erb.