Class: WellValidator

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

Overview

Failed validations return unprocessable_entity

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



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

def validate(record)
  return unless record.base_used_aliquots.many?

  validations = %i[validate_tag_presence validate_tag_uniqueness]

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

    send(validation, record)
  end
end

#validate_tag_presence(record) ⇒ Object



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

def validate_tag_presence(record)
  all_tags = record.base_used_aliquots.collect(&:tag)

  return unless all_tags.empty? || all_tags.any?(nil)

  record.errors.add(:tags, 'are missing from the libraries')
  nil
end

#validate_tag_uniqueness(record) ⇒ Object



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

def validate_tag_uniqueness(record)
  all_tags = record.base_used_aliquots.collect(&:tag)

  return if all_tags.length == all_tags.uniq.length

  record.errors.add(:tags, "are not unique within the libraries for well #{record.position}")
  nil
end