diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 8f2d4df..afaf084 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -51,7 +51,8 @@ # GET /dmptemplates/1 def admin_template - @template = Template.find(params[:id]) + @template = Template.includes(:org, phases: [sections: [questions: [:question_options, :question_format, + :suggested_answers]]]).find(params[:id]) # check to see if this is a funder template needing customized if @template.org_id != current_user.org_id # definitely need to deep_copy the given template @@ -72,7 +73,7 @@ end end end - customizations = Template.includes(phases: [sections: [questions: :suggested_answers ]]).where(org_id: current_user.org_id, customization_of: @template.dmptemplate_id).order(version: :desc) + customizations = Template.includes(:org, phases: [sections: [questions: :suggested_answers ]]).where(org_id: current_user.org_id, customization_of: @template.dmptemplate_id).order(version: :desc) if customizations.present? # existing customization to port over max_version = customizations.first @@ -145,6 +146,8 @@ @template = new_version end authorize @template + # once the correct template has been generated, we convert it to hash + @hash = @template.to_hash end diff --git a/app/views/templates/_show_phases_sections.html.erb b/app/views/templates/_show_phases_sections.html.erb index e802f08..1338717 100644 --- a/app/views/templates/_show_phases_sections.html.erb +++ b/app/views/templates/_show_phases_sections.html.erb @@ -11,10 +11,10 @@
- <% if phase.sections.length > 0 %> + <% if phase_hash[:sections].length > 0 %> <%= link_to _('Preview'), admin_preview_phase_path(id: phase.id), class: 'btn btn-primary'%> <% end %> - <% if !phase.template.published? %> + <% if !template.published? %> <%= link_to _('Delete'), admin_destroy_phase_path(phase_id: phase.id), confirm: _("You are about to delete '%{phase_title}'. This will affect versions, sections and questions linked to this phase. Are you sure?") % { :phase_title => phase.title }, method: :delete, class: "btn btn-primary"%> <% end %> @@ -26,7 +26,7 @@ <%= link_to b_label, admin_show_phase_path(id: phase.id, edit: (b_label == _('org_admin.templates.edit_phase_label'))), class: "btn btn-primary" %>
- <% if phase.sections.any? %> + <% if phase_hash[:sections].present? %> @@ -35,18 +35,18 @@ - <% phase.sections.order("number ASC").each do |section| %> + <% (1..phase_hash[:sections].length).each do |section_no| %> + <% section = phase_hash[:sections][section_no] %>
-

<%= section.title %>

+

<%= section[:data].title %>

- <% if section.questions.any? %> - <% questions = section.questions.order("number ASC") %> + <% if section[:questions].present? %>
    - <% questions.each do |ques|%> + <% (1..section[:questions].length).each do |question_no|%>
  • - - <%= raw ques.text %> + - <%= raw section[:questions][question_no][:data].text %>
  • <% end %>
diff --git a/app/views/templates/admin_template.html.erb b/app/views/templates/admin_template.html.erb index 6558be2..c515865 100644 --- a/app/views/templates/admin_template.html.erb +++ b/app/views/templates/admin_template.html.erb @@ -14,48 +14,49 @@
-<%= render partial: "templates/admin_nav_tabs", locals: {template: @template, active: "show_template"} %> +<%= render partial: "templates/admin_nav_tabs", locals: {template: @template, hash: @hash, active: "show_template"} %>
- <% if @template.org.funder? || current_user.org.funder? then %> + <% if @hash[:template][:org].funder? || current_user.org.funder? %> - <%end%> + <% end %>
- <%= render partial: "templates/show_template", locals: {template: @template}%> + <%= render partial: "templates/show_template", locals: {template: @template, hash: @hash}%>
- <% if !@template.phases.nil? then %> - <% if @template.phases.count == 1 then %> - <% @template.phases.each do |phase| %> + <% if @hash[:template][:phases].present? %> + <% if @hash[:template][:phases].length == 1 %> + <% @hash[:template][:phases].each do |phase_no, phase| %> - <%= render partial: 'templates/show_phases_sections', locals: {phase: phase}%> - <%end%> - <%else%> - <% @template.phases.order(:number).each do |phase| %> + <%= render partial: 'templates/show_phases_sections', locals: {phase: phase[:data], phase_hash: phase, template: @template} %> + <% end %> + <% else %> + <% (1..@hash[:template][:phases].length).each do |phase_no| %> + <% phase = @hash[:template][:phases][phase_no] %>
- <%= render partial: 'templates/show_phases_sections', locals: {phase: phase}%> + <%= render partial: 'templates/show_phases_sections', locals: {phase: phase[:data], phase_hash: phase, template: @template}%>
- <%end%> - <%end%> - <%end%> + <% end %> + <% end %> + <% end %>
<%= tinymce content_css: asset_path('application.css') %>