Class: TagValidator

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

Overview

TagValidator if the pool only has one library no need to check if there is more than one library each one will require a tag if there is more than one library each tag will need to be unique

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'app/validators/tag_validator.rb', line 8

def validate(record)
  # Since this is shared between PacBio (with aliquots) and ONT (with libraries)
  # We need to check which method to call
  if record.respond_to?(:used_aliquots)
    validate_used_aliquots(record)
  else
    validate_libraries(record)
  end
end

#validate_libraries(record) ⇒ Object



26
27
28
29
30
31
32
# File 'app/validators/tag_validator.rb', line 26

def validate_libraries(record)
  return if record.libraries.length < 2

  tags = record.libraries.collect(&:tag)
  record.errors.add(:tags, 'must be present on all libraries') if tags.any?(&:nil?)
  record.errors.add(:tags, 'contain duplicates') unless tags.length == tags.uniq.length
end

#validate_used_aliquots(record) ⇒ Object



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

def validate_used_aliquots(record)
  return if record.used_aliquots.length < 2

  tags = record.used_aliquots.collect(&:tag)
  record.errors.add(:tags, 'must be present on all libraries') if tags.any?(&:nil?)
  record.errors.add(:tags, 'contain duplicates') unless tags.length == tags.uniq.length
end