Sha256: 30ac04d64895704b71de83ec9a49a1be612b7816acfad77a6a0aef4d7f916546

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true
module Hyrax
  ##
  # Validates that the record passes the multiple membership requirements for collections.
  class CollectionMembershipValidator < ActiveModel::Validator
    ##
    # @param multiple_membership_checker
    def initialize(options = {})
      @multiple_membership_checker = options[:multiple_membership_checker] || Hyrax::MultipleMembershipChecker
      super(options)
    end

    def validate(record)
      # collections-in-collections do not have multi-membership restrictions
      return true if record.is_a? Hyrax::Forms::PcdmCollectionForm
      checker = @multiple_membership_checker.new(item: nil)
      ids = collections_ids(record)

      error = checker.check(collection_ids: ids)
      record.errors.add(:member_of_collection_ids, error) if error
    end

    private

    def collections_ids(record)
      collection_ids = record.member_of_collection_ids.reject(&:blank?)

      if record.member_of_collections_attributes.present?
        record.member_of_collections_attributes
              .each do |_k, h|
                next if h["_destroy"] == "true"
                collection_ids += [Valkyrie::ID.new(h["id"])]
              end
      end

      collection_ids
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hyrax-5.0.4 app/validators/hyrax/collection_membership_validator.rb
hyrax-5.0.3 app/validators/hyrax/collection_membership_validator.rb
hyrax-5.0.2 app/validators/hyrax/collection_membership_validator.rb
hyrax-5.0.1 app/validators/hyrax/collection_membership_validator.rb
hyrax-5.0.0 app/validators/hyrax/collection_membership_validator.rb
hyrax-5.0.0.rc3 app/validators/hyrax/collection_membership_validator.rb
hyrax-5.0.0.rc2 app/validators/hyrax/collection_membership_validator.rb