Skip to content

Commit

Permalink
patch for issue saving related works on follow up page
Browse files Browse the repository at this point in the history
  • Loading branch information
briri committed Apr 21, 2023
1 parent 6bb1050 commit 50807bf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
15 changes: 15 additions & 0 deletions app/controllers/dmptool/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions app/models/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ class Plan < ApplicationRecord

accepts_nested_attributes_for :research_outputs

accepts_nested_attributes_for :related_identifiers

# ===============
# = Validations =
# ===============
Expand Down
3 changes: 2 additions & 1 deletion app/models/related_identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/views/branded/related_identifiers/_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tooltip = _('Add another research output to this plan.')
<div class="col-md-12 citation">
<span class="col-md-10">
<%# We need to add each identifiers fields here so that they are submitted and retained! %>
<% %w[citation identifier_type relation_type value work_type].each do |attr| %>
<% %w[id citation identifier_type relation_type value work_type].each do |attr| %>
<%= hidden_field_tag "plan[related_identifiers_attributes[#{idx + 1}][#{attr}]]",
related_identifier.send(attr.to_sym) %>
<% end %>
Expand Down

0 comments on commit 50807bf

Please sign in to comment.