Skip to content

Commit

Permalink
fix editing contributions on Asset edit form
Browse files Browse the repository at this point in the history
Properly edit Contribution and ContributionResource records from the
Asset form in the way each model expects. Also, properly create new
ContributionResource records while ensuring blank form fields don't get
saved to the database
  • Loading branch information
bkiahstroud committed Dec 24, 2024
1 parent 15641f0 commit 98a87f2
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions app/transactions/ams/steps/handle_contributors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,43 @@ def create_or_update_contributions(change_set, contributions)

to_destroy = ActiveModel::Type::Boolean.new.cast(param_contributor['_destroy'])
if to_destroy
# FIXME: removing the member doesn't seem to work, destroy instead of unlink?
destroys << param_contributor[:id]
next
end

if param_contributor[:id].present?
contributor = Hyrax.query_service.find_by(id: param_contributor[:id])
if contributor.is_a?(Contribution)
param_contributor.delete(:id)
contributor.attributes.merge!(param_contributor)
contributor_resource = Hyrax.persister.save(resource: contributor)
Hyrax.publisher.publish('object.metadata.updated', object: contributor_resource, user: user)
inserts << contributor_resource.id
else
# param_contributor.slice(:contributor_role, :contributor, :affiliation, :portrayal).each do |attr, value|
# contributor.send("#{attr}=", value)
# end
# Hyrax.persister.save(resource: contributor)
# Hyrax.index_adapter.save(resource: contribution)
# Hyrax.publisher.publish('object.metadata.updated', object: contributor, user: user)
contribution_form = Hyrax::Forms::ResourceForm.for(contributor)
sanitized_params = param_contributor.slice(:contributor_role, :contributor, :affiliation, :portrayal)
contribution_form.validate(sanitized_params)
Hyrax::Transactions::Container['change_set.apply'].call(contribution_form)
inserts << contributor.id
end
else
flat_values = param_contributor.slice(:contributor_role, :affiliation, :portrayal).values + param_contributor[:contributor]
next if flat_values.none?(&:present?)

contributor = Contribution.find(param_contributor[:id]) if param_contributor[:id].present?
if contributor
param_contributor.delete(:id)
contributor.attributes.merge!(param_contributor)
contributor_resource = Hyrax.persister.save(resource: contributor)
Hyrax.publisher.publish('object.metadata.updated', object: contributor_resource, user: user)
inserts << contributor_resource.id
next
contribution_attrs = param_contributor.symbolize_keys.except(:id) # IDs are blank for new records submitted by form
contribution_resource = Hyrax.persister.save(resource: ContributionResource.new(contribution_attrs))
Hyrax.index_adapter.save(resource: contribution_resource)
Hyrax.publisher.publish('object.deposited', object: contribution_resource, user: user)
Hyrax::AccessControlList.copy_permissions(source: target_permissions, target: contribution_resource)
inserts << contribution_resource.id
end
contribution_resource = Hyrax.persister.save(resource: ContributionResource.new(param_contributor.symbolize_keys))
Hyrax.index_adapter.save(resource: contribution_resource)
Hyrax.publisher.publish('object.deposited', object: contribution_resource, user: user)
Hyrax::AccessControlList.copy_permissions(source: target_permissions, target: contribution_resource)
inserts << contribution_resource.id
end

update_members(change_set, inserts, destroys)
Expand Down

0 comments on commit 98a87f2

Please sign in to comment.