Class: ProgramsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/models/concerns/programs_validator.rb

Constant Summary collapse

PROGRAMS_LABELS =

attribute should be of the form: {'pcr 1' => { 'name' => “pcr1 program”, 'duration' => 45 },

'pcr 2' => { 'name' => "pcr2 program" , 'duration' => 20 }}
['pcr 1', 'pcr 2'].freeze
PROGRAMS_PARAMS =
%w[name duration].freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


11
12
13
14
15
16
17
18
19
# File 'app/models/concerns/programs_validator.rb', line 11

def validate_each(record, attribute, value)
  value.each do |program, params|
    record.errors.add attribute, "invalid label #{program}" unless program.in?(PROGRAMS_LABELS)
    params.each do |key, val|
      record.errors.add attribute, "invalid attribute #{key}" unless key.in?(PROGRAMS_PARAMS)
      validate_duration(record, attribute, val) if key == 'duration'
    end
  end
end