Module: DelegateValidation

Defined in:
app/models/delegate_validation.rb

Overview

Delegate validation is all about enabling one class to validate the information within an instance of another class. The case driving this is the ability for a Submission to validate that the request options provided by the user are valid for the RequestType instances that the submission is going to use. In that case the RequestType#delegate_validator returns a class that can then be used to validate the request options. Because RequestType isn't subclassed it actually delegates to the Request class that it'll instantiate, so you can find examples of the delegator stuff in SequencingRequest and LibraryCreationRequest

Defined Under Namespace

Classes: AlwaysValidValidator, CompositeValidator, Validator

Instance Method Summary collapse

Instance Method Details

#delegate_validation(*args) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/delegate_validation.rb', line 8

def delegate_validation(*args)
  options           = args.extract_options!
  delegation_target = options.delete(:to) or raise StandardError, 'Cannot delegate validation without :to!'
  attribute_tag     = options[:as]
  args.push(options)

  validates_each(*args) do |record, _attr, value|
    validator = record.send(:"#{delegation_target}_delegate_validator").new(value)
    validator.valid?.tap do
      validator.errors.messages.each do |attrib, message|
        record.errors.add("#{attribute_tag}.#{attrib}", message.join('; '))
      end
    end
  end
end