app/forms/hyrax/forms/resource_form.rb in hyrax-3.4.1 vs app/forms/hyrax/forms/resource_form.rb in hyrax-3.4.2

- old
+ new

@@ -97,11 +97,11 @@ # pcdm relationships property :admin_set_id, prepopulator: ->(_opts) { self.admin_set_id = Hyrax::AdminSetCreateService.find_or_create_default_admin_set.id.to_s } property :in_works_ids, virtual: true, prepopulator: InWorksPrepopulator property :member_ids, default: [], type: Valkyrie::Types::Array property :member_of_collection_ids, default: [], type: Valkyrie::Types::Array - property :member_of_collections_attributes, virtual: true + property :member_of_collections_attributes, virtual: true, populator: :in_collections_populator validates_with CollectionMembershipValidator property :representative_id, type: Valkyrie::Types::String property :thumbnail_id, type: Valkyrie::Types::String property :rendering_ids, default: [], type: Valkyrie::Types::Array @@ -205,9 +205,23 @@ def display_additional_fields? secondary_terms.any? end private + + def in_collections_populator(fragment:, **_options) + adds = [] + deletes = [] + fragment.each do |_, h| + if h["_destroy"] == "true" + deletes << Valkyrie::ID.new(h["id"]) + else + adds << Valkyrie::ID.new(h["id"]) + end + end + + self.member_of_collection_ids = ((member_of_collection_ids + adds) - deletes).uniq + end # https://trailblazer.to/2.1/docs/reform.html#reform-populators-populator-collections def permission_populator(collection:, index:, **) Hyrax::Forms::Permission.new(collection[index]) end