diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb
index 8b762fd..b590d41 100644
--- a/app/controllers/answers_controller.rb
+++ b/app/controllers/answers_controller.rb
@@ -22,7 +22,6 @@
@old_answer = nil
if @answer.nil?
- logger.debug "didn't find answer so creating a new one"
@answer = Answer.new(params[:answer])
authorize @answer
@answer.save
diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb
index 6133a45..8c66763 100644
--- a/app/controllers/phases_controller.rb
+++ b/app/controllers/phases_controller.rb
@@ -4,17 +4,74 @@
after_action :verify_authorized
- # GET /plans/PLANID/phases/PHASEID/edit
+ # GET /plans/:plan_id/phases/:id/edit
def edit
- @plan = Plan.find(params[:plan_id])
+ @plan = Plan.eager_load2(params[:plan_id])
authorize @plan
- @plan_data = @plan.to_hash
-
phase_id = params[:id].to_i
- @phase = Phase.find(phase_id)
- @phase_data = @plan_data["template"]["phases"].select {|p| p["id"] == phase_id}.first
+ @phase = @plan.template.phases.select {|p| p.id == phase_id}.first
+
+ # the eager_load pulls in ALL answers
+ # need to restrict to just ones for this plan
+ @plan.template.phases.each do |phase|
+ phase.sections do |section|
+ section.questions.each do |question|
+ question.answers = question.answers.to_a.select {|answer| answer.plan_id == @plan.id}
+ end
+ end
+ end
+
+ # Now we need to get all the themed guidance for the plan.
+ # TODO: think this through again, there may be a better way to do this.
+ #
+ # Ultimately we are heading to a map from question id to theme to guidance.
+ #
+ # get the ids of the dynamically selected guidance groups
+ # and keep a map of them so we can extract the names later
+ guidance_groups_ids = @plan.plan_guidance_groups.select{|pgg| pgg.selected}.map{|pgg| pgg.guidance_group.id}
+ guidance_groups = GuidanceGroup.includes({guidances: :themes}).find(guidance_groups_ids)
+
+ # create a map from theme to array of guidances
+ # where guidance is a hash with the text and the org name
+ theme_guidance = {}
+
+ guidance_groups.each do |guidance_group|
+ guidance_group.guidances.each do |guidance|
+ guidance.themes.each do |theme|
+ title = theme.title
+ if !theme_guidance.has_key?(title)
+ theme_guidance[title] = Array.new
+ end
+ theme_guidance[title] << {
+ text: guidance.text,
+ org: guidance_group.name
+ }
+ end
+ end
+ end
+
+ # create hash from question id to theme to guidance array
+ # so when we arerendering a question we can grab the guidance out of this
+ #
+ # question_guidance = {
+ # question.id => {
+ # theme => [ {text: "......", org: "....."} ]
+ # }
+ # }
+ @question_guidance = {}
+ @plan.questions.each do |question|
+ qg = {}
+ question.themes.each do |t|
+ title = t.title
+ qg[title] = theme_guidance[title] if theme_guidance.has_key?(title)
+ end
+ if !@question_guidance.has_key?(question.id)
+ @question_guidance[question.id] = Array.new
+ end
+ @question_guidance[question.id] = qg
+ end
if !user_signed_in? then
respond_to do |format|
@@ -27,7 +84,7 @@
# GET /plans/PLANID/phases/PHASEID/status.json
def status
- @plan = Plan.find(params[:plan_id])
+ @plan = Plan.eager_load(params[:plan_id])
authorize @plan
if user_signed_in? && @plan.readable_by?(current_user.id) then
respond_to do |format|
@@ -42,7 +99,7 @@
#show and edit a phase of the template
def admin_show
- @phase = Phase.find(params[:id])
+ @phase = Phase.eager_load(params[:id])
authorize @phase
@edit = params[:edit] == "true" ? true : false
#verify if there are any sections if not create one
diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb
index 43be23d..abc7b8f 100644
--- a/app/controllers/plans_controller.rb
+++ b/app/controllers/plans_controller.rb
@@ -104,15 +104,13 @@
# GET /plans/show
def show
- @plan = Plan.find(params[:id])
+ @plan = Plan.eager_load(params[:id])
authorize @plan
- @plan_data = @plan.to_hash
-
@editing = params[:editing] && @plan.administerable_by?(current_user.id)
@selected_guidance_groups = []
- all_guidance_groups = @plan_data["plan_guidance_groups"]
- @selected_guidance_groups = all_guidance_groups.map{ |pgg| [ pgg["guidance_group"]["name"], pgg["guidance_group"]["id"], :checked => pgg["selected"] ] }
+ all_guidance_groups = @plan.plan_guidance_groups
+ @selected_guidance_groups = all_guidance_groups.map{ |pgg| [ pgg.guidance_group.name, pgg.guidance_group.id, :checked => pgg.selected ] }
@selected_guidance_groups.sort!
if user_signed_in? && @plan.readable_by?(current_user.id) then
diff --git a/app/models/plan.rb b/app/models/plan.rb
index 4c5f974..ba76aa4 100644
--- a/app/models/plan.rb
+++ b/app/models/plan.rb
@@ -995,6 +995,35 @@
end
+ # the following two methods are for eager loading. One gets used for the plan/show
+ # page and the oter for the plan/edit. The difference is just that one pulls in more than
+ # the other.
+ # TODO: revisit this and work out for sure that maintaining the difference is worthwhile.
+ # it may not be. Also make sure nether is doing more thanit needs to.
+ #
+ def self.eager_load(id)
+ Plan.includes(
+ [{template: [
+ {phases: {sections: {questions: :answers}}},
+ {customizations: :org}
+ ]},
+ {plan_guidance_groups: {guidance_group: :guidances}}
+ ]).find(id)
+ end
+
+ def self.eager_load2(id)
+ Plan.includes(
+ [{template: [
+ {phases: {sections: {questions: [{answers: :notes}, :suggested_answers, :question_format, :themes]}}},
+ {customizations: :org},
+ :org
+ ]},
+ {plan_guidance_groups: {guidance_group: {guidances: :themes}}},
+ {questions: :themes}
+ ]).find(id)
+ end
+
+
private
diff --git a/app/models/question.rb b/app/models/question.rb
index c36aaef..4533cc6 100644
--- a/app/models/question.rb
+++ b/app/models/question.rb
@@ -50,6 +50,10 @@
return format.option_based
end
+ def plan_answers(plan_id)
+ return self.answers.to_a.select{|ans| ans.plan_id == plan_id}
+ end
+
##
# deep copy the given question and all it's associations
#
diff --git a/app/models/section.rb b/app/models/section.rb
index 87e98b6..b88ee79 100644
--- a/app/models/section.rb
+++ b/app/models/section.rb
@@ -24,6 +24,14 @@
"#{title}"
end
+ def num_answered_questions(plan_id)
+ n = 0
+ self.questions.each do |question|
+ n += question.plan_answers(plan_id).select{|answer| answer.text.present?}.count
+ end
+ return n
+ end
+
##
# deep copy of the given section and all it's associations
#
diff --git a/app/views/phases/_add_note.html.erb b/app/views/phases/_add_note.html.erb
index 2b59859..190f89b 100644
--- a/app/views/phases/_add_note.html.erb
+++ b/app/views/phases/_add_note.html.erb
@@ -1,12 +1,9 @@
-<% new_note = Note.new
- if !answer.nil?
- answerid = answer["id"]
- else
- answerid = answer_obj.id
- end
+<%
+ new_note = Note.new
+ answerid = answer.id
%>
<%= form_for :new_note,
@@ -14,8 +11,8 @@
:html=>{:method=>:post, :id => "new_note_form_#{answerid}", :class => "add_note_form"} do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :answer_id, :value => answerid %>
- <%= f.hidden_field :question_id, :value => question["id"] %>
- <%= f.hidden_field :plan_id, :value => plan_data["id"] %>
+ <%= f.hidden_field :question_id, :value => question.id %>
+ <%= f.hidden_field :plan_id, :value => plan_id %>
<%= text_area_tag("#{answerid}new_note_text".to_sym, "" , class: "tinymce") %>
diff --git a/app/views/phases/_answer_form.html.erb b/app/views/phases/_answer_form.html.erb
index dee0e31..5b498fe 100644
--- a/app/views/phases/_answer_form.html.erb
+++ b/app/views/phases/_answer_form.html.erb
@@ -8,56 +8,55 @@
- <%= raw suggested_answer["text"] %> -
++ <%= raw suggested_answer.text %> +
+