Skip to content

Commit 609d5df

Browse files
committed
Refactor plan fetching in AnswersController action
- This refactor simply applies DRY principles to some of the plan fetching code in `AnswersController#create_or_update`.
1 parent d5bde03 commit 609d5df

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

app/controllers/answers_controller.rb

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,7 @@ def create_or_update
8585
# check should probably happen on create/update
8686
return unless @answer.present?
8787

88-
@plan = Plan.includes(
89-
sections: {
90-
questions: %i[
91-
answers
92-
question_format
93-
]
94-
}
95-
).find(p_params[:plan_id])
88+
@plan = fetch_plan_with_associations(p_params[:plan_id])
9689
@question = @answer.question
9790
@section = @plan.sections.find_by(id: @question.section_id)
9891
template = @section.phase.template
@@ -108,14 +101,7 @@ def create_or_update
108101
end
109102
end
110103
# Now update @plan after removing answers of questions removed from the plan.
111-
@plan = Plan.includes(
112-
sections: {
113-
questions: %i[
114-
answers
115-
question_format
116-
]
117-
}
118-
).find(p_params[:plan_id])
104+
@plan = fetch_plan_with_associations(p_params[:plan_id])
119105

120106
# Now get list of question ids to remove based on remaining answers.
121107
remove_list_question_ids = remove_list(@plan)
@@ -196,6 +182,17 @@ def permitted_params
196182
end
197183
# rubocop:enable Metrics/AbcSize
198184

185+
def fetch_plan_with_associations(plan_id)
186+
Plan.includes(
187+
sections: {
188+
questions: %i[
189+
answers
190+
question_format
191+
]
192+
}
193+
).find(plan_id)
194+
end
195+
199196
def check_answered(section, q_array, all_answers)
200197
n_qs = section.questions.count { |question| q_array.include?(question.id) }
201198
n_ans = all_answers.count { |ans| q_array.include?(ans.question.id) and ans.answered? }

0 commit comments

Comments
 (0)