Class: Hyrax::FlexibleSchemaValidators::ExistingRecordsValidator

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/flexible_schema_validators/existing_records_validator.rb

Overview

Validates that classes with existing records in the repository are not removed from the profile.

Instance Method Summary collapse

Constructor Details

#initialize(profile, required_classes, errors) ⇒ ExistingRecordsValidator

Returns a new instance of ExistingRecordsValidator.

Parameters:

  • profile (Hash)

    M3 profile data

  • required_classes (Array<String>)

    Foundational classes that must be present

  • errors (Array<String>)

    Array to append validation errors to



10
11
12
13
14
# File 'app/services/hyrax/flexible_schema_validators/existing_records_validator.rb', line 10

def initialize(profile, required_classes, errors)
  @profile = profile
  @required_classes = required_classes
  @errors = errors
end

Instance Method Details

#validate!void

This method returns an undefined value.

Validates that no classes with existing records in the repository have been removed from the profile.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/hyrax/flexible_schema_validators/existing_records_validator.rb', line 20

def validate!
  profile_classes_set = Set.new(@profile.fetch('classes', {}).keys)
  classes_with_records = []
  potential_existing_classes.each do |model_class|
    model_identifier = model_class.to_s
    counterpart_identifier = counterpart_for(model_identifier)

    # If this model or its counterpart is in the profile, we don't need to check it for removal.
    next if profile_classes_set.include?(model_identifier) || profile_classes_set.include?(counterpart_identifier)

    # If it's not in the profile, check if it has records.
    classes_with_records << model_identifier if model_has_records?(model_class, model_identifier)
  end

  return if classes_with_records.empty?

  @errors << "Classes with existing records cannot be removed from the profile: #{classes_with_records.uniq.join(', ')}."
end