diff --git a/app/controllers/dmptool/plans_controller.rb b/app/controllers/dmptool/plans_controller.rb index 68198e4aaf..fd42e13514 100644 --- a/app/controllers/dmptool/plans_controller.rb +++ b/app/controllers/dmptool/plans_controller.rb @@ -34,6 +34,21 @@ def follow_up_update authorize @plan attrs = plan_params + + # Save the related_identifiers first. For some reason Rails is auto deleting them and then re-adding + # if you just pass in the params as is :/ + # + # So delete removed ones, add new ones, and leave the others alone + ids = attrs[:related_identifiers_attributes].to_h.values.map { |item| item['id'] }.compact + @plan.related_identifiers.reject { |identifier| ids.include?(identifier.id.to_s) }.each(&:destroy) + + attrs[:related_identifiers_attributes].each do |idx, related_identifier| + next if related_identifier[:id].present? || related_identifier[:value].blank? + + RelatedIdentifier.create(related_identifier.merge({ identifiable: @plan })) + end + attrs.delete(:related_identifiers_attributes) + @plan.grant = plan_params[:grant] attrs.delete(:grant) diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 40790f7de0..cc8ef5407f 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -485,7 +485,7 @@ def plan_params grant: %i[name value], org: %i[id org_id org_name org_sources org_crosswalk], funder: %i[id org_id org_name org_sources org_crosswalk], - related_identifiers_attributes: %i[id work_type value]) + related_identifiers_attributes: %i[id work_type value citation identifier_type relation_type]) end # different versions of the same template have the same family_id diff --git a/app/models/plan.rb b/app/models/plan.rb index d20a9ff230..c6a1cb34d9 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -153,8 +153,6 @@ class Plan < ApplicationRecord accepts_nested_attributes_for :research_outputs - accepts_nested_attributes_for :related_identifiers - # =============== # = Validations = # =============== diff --git a/app/models/related_identifier.rb b/app/models/related_identifier.rb index 9d3fe8650b..fbbcd9f957 100644 --- a/app/models/related_identifier.rb +++ b/app/models/related_identifier.rb @@ -121,7 +121,8 @@ def load_citation citation.nil? wrk_type = work_type == 'supplemental_information' ? '' : work_type # Use the UC3Citation service to fetch the citation for the DOI - self.citation = fetch_citation(doi: value, work_type: wrk_type) # , debug: true) + self.citation = fetch_citation(doi: value, work_type: wrk_type) #, debug: true) + self.save end end end diff --git a/app/views/branded/related_identifiers/_edit.html.erb b/app/views/branded/related_identifiers/_edit.html.erb index a6bce7c538..ebd5c8e8a0 100644 --- a/app/views/branded/related_identifiers/_edit.html.erb +++ b/app/views/branded/related_identifiers/_edit.html.erb @@ -18,7 +18,7 @@ tooltip = _('Add another research output to this plan.')