Class: QcReceptionsFactoryValidator

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

Overview

Failed validations return unprocessable_entity These are being validated before any entity is created

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'app/validators/qc_receptions_factory_validator.rb', line 6

def validate(record)
  validations = %i[validate_qc_results_list validate_assay_type]

  validations.each do |validation|
    next if record.errors.present?

    send(validation, record)
  end
end

#validate_assay_type(record) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/validators/qc_receptions_factory_validator.rb', line 26

def validate_assay_type(record)
  # Iterates through the qc_results_list array objects
  # checks if any qc attribute matches the assay type key
  # add error if not
  assay_types = QcAssayType.where(used_by: options[:used_by]).pluck(:id, :key)
  assay_types_hash = assay_types.to_h.invert
  record.qc_results_list.each do |qc_hash|
    return nil if qc_hash.any? { |qc, _value| assay_types_hash.keys.include? qc }
  end
  record.errors.add :qc_results_list, :invalid
  nil
end

#validate_qc_results_list(record) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'app/validators/qc_receptions_factory_validator.rb', line 16

def validate_qc_results_list(record)
  # Add error if the array is not empty
  # or all the objects inside the array are empty
  if record.qc_results_list.empty? ||
     record.qc_results_list.all?(&:empty?)
    record.errors.add :qc_results_list, :empty_array
  end
  nil
end