Class: ArrayEnum::SubsetValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/array_enum/subset_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/array_enum/subset_validator.rb', line 4

def validate_each(record, attribute, value)
  wrapped_value = [value].flatten # Handles nil value
  subset = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter

  diff = wrapped_value.reject { |element| subset.include?(element) }

  unless diff.empty?
    record.errors.add(attribute, :inclusion, **options.except(:in, :within).merge!(value: diff))
  end
end