Skip to content

Commit

Permalink
Merge pull request #11 from AdrYne01/refactor_set_questionnaire
Browse files Browse the repository at this point in the history
refactor to before action
  • Loading branch information
bdevine2 committed Apr 16, 2024
2 parents 3540db1 + e882f7b commit 364c7a2
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/controllers/advice_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class AdviceController < ApplicationController
before_action :set_questionnaire, only: %i[ edit_advice save_advice ]
# Advice_controller first checks whether current user has TA privileges or not by implementing action_allowed? method. Secondly it sets the number of advices based on score and sort it in descending order. Then it checks four conditions for the advices.
# 1. If number of advices is not equal to given advices
# 2. If the sorted advices is empty
Expand Down Expand Up @@ -26,9 +27,6 @@ def invalid_advice?(sorted_advice, num_advices, question)
# Separate methods were introduced to calculate the number of advices and sort the advices related to the current question attribute
# This is done to adhere to Single Responsibility Principle
def edit_advice
# Stores the questionnaire with given id in URL
@questionnaire = Questionnaire.find(params[:id])

# For each question in a questionnaire, this method adjusts the advice size if the advice size is <,> number of advices or
# the max or min score of the advices does not correspond to the max or min score of questionnaire respectively.
@questionnaire.questions.each do |question|
Expand Down Expand Up @@ -64,8 +62,6 @@ def sort_question_advices(question)

# save the advice for a questionnaire
def save_advice
# Stores the questionnaire with given id in URL
@questionnaire = Questionnaire.find(params[:id])
begin
# checks if advice is present or not
unless params[:advice].nil?
Expand All @@ -83,4 +79,12 @@ def save_advice
# regardless of action above redirect to edit and show flash message if one exists
redirect_to action: 'edit_advice', id: params[:id]
end

private

# Common code for set questionnaire
def set_questionnaire
# Stores the questionnaire with given id in URL
@questionnaire = Questionnaire.find(params[:id])
end
end

0 comments on commit 364c7a2

Please sign in to comment.