Class: PipelineValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/pipeline_validator.rb

Overview

Validates that attribute belongs to the configured pipeline Requires that the associated attribute responds to

Examples:

validates :library_type, pipeline: :ont

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PipelineValidator

Returns a new instance of PipelineValidator.

Raises:

  • (ArgumentError)


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

def initialize(options)
  super

  # We'll give some helpful errors if we configure it wrong
  raise ArgumentError, 'No target pipeline specified' if expected.nil?
  raise ArgumentError, "#{expected} is not a recognised pipeline" unless valid_pipeline?
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



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

def validate_each(record, attribute, value)
  return if value.blank?

  actual = value.pipeline
  return if actual == expected.to_s

  record.errors.add(attribute, :pipeline_invalid, expected:, actual:)
end