Sha256: 0d67f75dbae3415d3017f6acfc74ab7bd0ff31954635610d5b456eba9355b2ad

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module Hyrax
  module Forms
    ##
    # A form for PCDM objects: resources which have collection relationships and
    # generally resemble +Hyrax::Work+.
    #
    # Although File Sets are technically also PCDM objects, they use a separate
    # form class: +Hyrax::Forms::FileSetForm+.
    class PcdmObjectForm < Hyrax::Forms::ResourceForm
      include Hyrax::FormFields(:core_metadata)

      include Hyrax::ContainedInWorksBehavior
      include Hyrax::DepositAgreementBehavior
      include Hyrax::LeaseabilityBehavior
      include Hyrax::PermissionBehavior

      property :on_behalf_of
      property :proxy_depositor

      # pcdm relationships
      property :admin_set_id, prepopulator: :admin_set_prepopulator
      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, 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

      # backs the child work search element;
      # @todo: look for a way for the view template not to depend on this
      property :find_child_work, default: nil, virtual: true

      private

      def admin_set_prepopulator
        self.admin_set_id ||= Hyrax::AdminSetCreateService.find_or_create_default_admin_set.id.to_s
      end

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hyrax-5.0.2 app/forms/hyrax/forms/pcdm_object_form.rb
hyrax-5.0.1 app/forms/hyrax/forms/pcdm_object_form.rb
hyrax-5.0.0 app/forms/hyrax/forms/pcdm_object_form.rb
hyrax-5.0.0.rc3 app/forms/hyrax/forms/pcdm_object_form.rb