diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index c19acc6..3ef036b 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -44,6 +44,44 @@ # GET /dmptemplates/1 def admin_template @template = Template.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 + new_customization = Template.deep_copy(@template) + new_customization.org_id = current_user.org_id + new_customization.published = false + new_customization.customization_of = @template.dmptemplate_id + # need to mark all Phases, questions, sections as not-modifiable + new_customization.phases.includes(sections: :questions).each do |phase| + phase.modifiable = false + phase.save! + phase.sections.each do |section| + section.modifiable = false + section.save! + section.questions.each do |question| + question.modifiable = false + question.save! + end + end + end + customizations = Template.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 + new_customization.dmptemplate_id = max_version.dmptemplate_id + new_customization.version = max_version.version + 1 + # port customization data...? + else + # first time customization + new_customization.version = 0 + new_customization.dmptemplate_id = loop do + random = rand 2147483647 # max int field in psql + break random unless Template.exists?(dmptemplate_id: random) + end + end + new_customization.save! + @template = new_customization + end authorize @template end @@ -59,13 +97,14 @@ @template.description = params["template-desc"] if @template.update_attributes(params[:template]) if @template.published - # create a new template version + # create a new template version if this template became published new_version = Template.deep_copy(@template) new_version.version = @template.version + 1 + new_version.published = false new_version.save! # if the organisation is a funder if @template.org.funder? - # do something about all the customizations + # do something about all the customizations? end end redirect_to admin_index_template_path(), notice: I18n.t('org_admin.templates.updated_message') diff --git a/app/models/question.rb b/app/models/question.rb index c2631a5..e086165 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -59,7 +59,7 @@ end question.suggested_answers.each do |suggested_answer| suggested_answer_copy = SuggestedAnswer.deep_copy(suggested_answer) - suggested_answer_copy.quesion_id = question_copy.id + suggested_answer_copy.question_id = question_copy.id suggested_answer_copy.save! end question.themes.each do |theme| diff --git a/app/models/template.rb b/app/models/template.rb index 87d79df..acdb11e 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -5,7 +5,7 @@ # Associations belongs_to :org has_many :plans - has_many :phases + has_many :phases, dependent: :destroy has_many :sections, through: :phases has_many :questions, through: :sections diff --git a/app/views/templates/_admin_nav_tabs.html.erb b/app/views/templates/_admin_nav_tabs.html.erb index d5cb6c6..de4c5fa 100644 --- a/app/views/templates/_admin_nav_tabs.html.erb +++ b/app/views/templates/_admin_nav_tabs.html.erb @@ -15,7 +15,7 @@ <% else %>
  • <% end %> - <%= link_to phase.title, admin_phase_template_path(phase) %> + <%= link_to phase.title, admin_phase_template_path(phase.id) %>
  • <% end %> diff --git a/app/views/templates/_edit_phase.html.erb b/app/views/templates/_edit_phase.html.erb index 1aefe61..ace20c9 100644 --- a/app/views/templates/_edit_phase.html.erb +++ b/app/views/templates/_edit_phase.html.erb @@ -1,6 +1,6 @@ -<%= form_for(phase, url: admin_updatephase_template_path(phase), html: { method: :put}) do |f| %> +<%= form_for(phase, url: admin_updatephase_template_path(phase.id), html: { method: :put}) do |f| %>

    <%= t('org_admin.templates.phase_details_label')%> diff --git a/app/views/templates/_edit_section.html.erb b/app/views/templates/_edit_section.html.erb index 22b7730..eb4abed 100644 --- a/app/views/templates/_edit_section.html.erb +++ b/app/views/templates/_edit_section.html.erb @@ -65,8 +65,8 @@ <% end %>
    <% @questions = section.questions.order("number")%> - <% if @questions.count > 0 %> - <% question_left = @questions.count %> + <% if @questions.length > 0 %> + <% question_left = @questions.length %> <% @questions.each do |question| %>
    "> @@ -93,7 +93,7 @@ <% if section.modifiable %> - <% if @questions.count != 0 %> + <% if @questions.length != 0 %>
    <% end%> diff --git a/app/views/templates/admin_index.html.erb b/app/views/templates/admin_index.html.erb index 81d0a82..0f7f193 100644 --- a/app/views/templates/admin_index.html.erb +++ b/app/views/templates/admin_index.html.erb @@ -48,7 +48,7 @@ <% if org_template.published %> <%= t("helpers.yes_label") %> <% elsif org_template.version > 0 && Template.where(dmptemplate_id: org_template.dmptemplate_id, published: true).present? %> - <% #there is a published version, but this version is not %> + <% #there is a published version, but this version is not (data access in view TODO: clean) %> <% if org_template.created_at < org_template.updated_at %> <%= t("org_admin.templates.unpublished_changes") %> <% else %> @@ -111,7 +111,7 @@ <% customized = 0 %> - <% template = org_template %> + <% template = org_template # template to be edited%> <% if @templates_customizations[org_template.dmptemplate_id].present? %> <% if @templates_customizations[org_template.dmptemplate_id].updated_at < org_template.updated_at %> <%= t("org_admin.templates.original_changed") %> @@ -142,7 +142,7 @@ <% elsif customized == 3 %> <% b_label = t("org_admin.templates.update_customisation") %> <% end %> - <%= link_to b_label, admin_template_template_path(org_template), class: "dmp_table_link" %> + <%= link_to b_label, admin_template_template_path(template), class: "dmp_table_link" %> <% end %> diff --git a/app/views/templates/admin_phase.html.erb b/app/views/templates/admin_phase.html.erb index d267086..08c4ce5 100644 --- a/app/views/templates/admin_phase.html.erb +++ b/app/views/templates/admin_phase.html.erb @@ -31,7 +31,7 @@
    <%= render partial: "show_phase", locals: {phase: @phase}%>
    - <% if @phase.modifiable then %> + <% if @phase.modifiable && @edit %> @@ -52,20 +52,18 @@
    - - <% if !@phase.template.published %> +<% if @edit %> + + - -