diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb index cfb8069..17215e8 100644 --- a/app/controllers/phases_controller.rb +++ b/app/controllers/phases_controller.rb @@ -6,14 +6,14 @@ # GET /plans/:plan_id/phases/:id/edit def edit + plan = Plan.load_for_phase(params[:plan_id], params[:id]) - @plan = Plan.load_for_phase(params[:plan_id], params[:id]) # authorization done on plan so found in plan_policy - authorize @plan + authorize plan phase_id = params[:id].to_i - @phase = @plan.template.phases.first - @readonly = !@plan.editable_by?(current_user.id) + phase = plan.template.phases.select {|p| p.id == phase_id}.first + readonly = !plan.editable_by?(current_user.id) # 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. @@ -22,7 +22,7 @@ # # 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.guidance_groups.map{|pgg| pgg.id} + guidance_groups_ids = plan.guidance_groups.map{|pgg| pgg.id} guidance_groups = GuidanceGroup.includes({guidances: :themes}).where(published: true, id: guidance_groups_ids) # create a map from theme to array of guidances @@ -46,35 +46,34 @@ end end - questions = [] - # Appends all the questions for a given phase into questions Array. - @phase.sections.each do |section| - section.questions.each do |question| - questions.push(question) - end - end - @question_guidance = {} - # Puts in question_guidance (key/value) entries where key is the question id and value is a hash. - # Each question id hash has (key/value) entries where key is a theme and value is an Array of {text, org} objects - # Example hash - # question_guidance = { question.id => - # { theme => [ {text: "......", org: "....."} ] } - # } - questions.each do |question| + # 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 - @question_guidance[question.id] = qg + 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| format.html { redirect_to edit_user_registration_path } end + else + render('/phases/edit', locals: { plan: plan, phase: phase, readonly: readonly, question_guidance: question_guidance }) end - end diff --git a/app/models/question.rb b/app/models/question.rb index 0a3e947..b92355d 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -115,6 +115,10 @@ return example_answer.first end + def first_example_answer + self.annotations.where(type: Annotation.types[:example_answer]).order(:created_at).first + end + ## # get guidance belonging to the current user's org for this question(need org # to distinguish customizations) diff --git a/app/views/annotations/_show.html.erb b/app/views/annotations/_show.html.erb index 792a720..8343f07 100644 --- a/app/views/annotations/_show.html.erb +++ b/app/views/annotations/_show.html.erb @@ -1,3 +1,4 @@ +<%# locals: {template, annotation, question } %>