Class: Hyrax::CollectionMembershipValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/hyrax/collection_membership_validator.rb

Overview

Validates that the record passes the multiple membership requirements for collections.

Instance Method Summary collapse

Constructor Details

#initialize(multiple_membership_checker: Hyrax::MultipleMembershipChecker, **options) ⇒ CollectionMembershipValidator

Returns a new instance of CollectionMembershipValidator.

Parameters:

  • multiple_membership_checker (defaults to: Hyrax::MultipleMembershipChecker)

8
9
10
11
# File 'app/validators/hyrax/collection_membership_validator.rb', line 8

def initialize(multiple_membership_checker: Hyrax::MultipleMembershipChecker, **options)
  @multiple_membership_checker = multiple_membership_checker
  super(options)
end

Instance Method Details

#validate(record) ⇒ Object


13
14
15
16
17
18
19
20
21
# File 'app/validators/hyrax/collection_membership_validator.rb', line 13

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)

  errors = Array(checker.check(collection_ids: ids))
  record.errors[:member_of_collection_ids].concat(errors)
end