Module: Hydra::Validations::EnumerableBehavior

Included in:
FormatValidator, InclusionValidator
Defined in:
lib/hydra/validations/enumerable_behavior.rb

Overview

EnumerableBehavior - a mixin for subclasses of ActiveModel::EachValidator adding validation for each member of an enumerable attribute value.

Behavior includes ‘fixing’ each error message to include the specific value which was invalid.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hydra/validations/enumerable_behavior.rb', line 12

def validate_each(record, attribute, value)
  if value.respond_to?(:each)
    value.each do |v| 
      prev = record.errors[attribute].size rescue 0
      super(record, attribute, v)
      messages = record.errors[attribute]
      if messages && messages.size > prev
        record.errors.add(attribute, fixed_message(v, messages.pop))
      end
    end
  else
    super
  end
end