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/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_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 %>