Skip to content

Commit cbe3cf0

Browse files
authored
Merge pull request #635 from DigitalCurationCentre/issue634
Issue634
2 parents 503c444 + 6248085 commit cbe3cf0

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

app/controllers/phases_controller.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class PhasesController < ApplicationController
77
# GET /plans/:plan_id/phases/:id/edit
88
def edit
99

10-
@plan = Plan.eager_load2(params[:plan_id])
10+
@plan = Plan.load_for_phase(params[:plan_id], params[:id])
1111
# authorization done on plan so found in plan_policy
1212
authorize @plan
1313

1414
phase_id = params[:id].to_i
15-
@phase = @plan.template.phases.select {|p| p.id == phase_id}.first
15+
@phase = @plan.template.phases.first
1616
@readonly = !@plan.editable_by?(current_user.id)
1717

1818
# Now we need to get all the themed guidance for the plan.
@@ -46,35 +46,35 @@ def edit
4646
end
4747
end
4848

49-
# create hash from question id to theme to guidance array
50-
# so when we arerendering a question we can grab the guidance out of this
51-
#
52-
# question_guidance = {
53-
# question.id => {
54-
# theme => [ {text: "......", org: "....."} ]
55-
# }
56-
# }
49+
questions = []
50+
# Appends all the questions for a given phase into questions Array.
51+
@phase.sections.each do |section|
52+
section.questions.each do |question|
53+
questions.push(question)
54+
end
55+
end
5756
@question_guidance = {}
58-
@plan.questions.each do |question|
57+
# Puts in question_guidance (key/value) entries where key is the question id and value is a hash.
58+
# Each question id hash has (key/value) entries where key is a theme and value is an Array of {text, org} objects
59+
# Example hash
60+
# question_guidance = { question.id =>
61+
# { theme => [ {text: "......", org: "....."} ] }
62+
# }
63+
questions.each do |question|
5964
qg = {}
6065
question.themes.each do |t|
6166
title = t.title
6267
qg[title] = theme_guidance[title] if theme_guidance.has_key?(title)
6368
end
64-
if !@question_guidance.has_key?(question.id)
65-
@question_guidance[question.id] = Array.new
66-
end
6769
@question_guidance[question.id] = qg
6870
end
6971

7072
if !user_signed_in? then
7173
respond_to do |format|
7274
format.html { redirect_to edit_user_registration_path }
73-
end
74-
end
75-
75+
end
7676
end
77-
77+
end
7878

7979
# GET /plans/PLANID/phases/PHASEID/status.json
8080
def status

app/models/plan.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -990,16 +990,15 @@ def self.eager_load(id)
990990
]).find(id)
991991
end
992992

993-
def self.eager_load2(id)
993+
def self.load_for_phase(id, phase_id)
994994
Plan.includes(
995-
[{template: [
995+
[template: [
996996
{phases: {sections: {questions: [{answers: :notes}, :annotations, :question_format, :themes]}}},
997997
{customizations: :org},
998998
:org
999-
]},
1000-
{plans_guidance_groups: {guidance_group: {guidances: :themes}}},
1001-
{questions: :themes}
1002-
]).find(id)
999+
],
1000+
plans_guidance_groups: {guidance_group: {guidances: :themes}}
1001+
]).where(id: id, phases: { id: phase_id }).first
10031002
end
10041003

10051004

0 commit comments

Comments
 (0)