Class: PartitionalValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/partitional/validator.rb

Constant Summary collapse

ALLOWED_OPTIONS =
%i[
  allow_nil
  allow_blank
  if
  unless
  message
  attributes
]

Instance Method Summary collapse

Instance Method Details

#check_validity!Object

Raises:

  • (ArgumentError)

13
14
15
16
17
18
19
# File 'lib/partitional/validator.rb', line 13

def check_validity!
  invalid_keys = []
  options.except(*ALLOWED_OPTIONS).each_pair do |key, value|
    invalid_keys.push("#{key}: #{value}")
  end
  raise ArgumentError, "#{invalid_keys.join(', ')} is invalid options." unless invalid_keys.empty?
end

#validate_each(record, attribute, value) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/partitional/validator.rb', line 21

def validate_each(record, attribute, value)
  return if options[:if].present? && !to_value(record, options[:if])
  return if options[:unless].present? && to_value(record, options[:unless])

  value.validate
  mapping = value.mapping

  value.errors.each do |attr, message|
    record.errors.add(mapping[attr], message)
  end
end