Class: ActiveModel::Validations::SubsetValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/validates_subset.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SubsetValidator

Returns a new instance of SubsetValidator.



18
19
20
21
22
23
24
25
26
# File 'lib/validates_subset.rb', line 18

def initialize(options)
  options[:superset] = options.delete(:subset)

  merged_options = {
    message: :subset
  }.merge(options)

  super(merged_options)
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Validate that the array is a subset of what we expect



35
36
37
# File 'lib/validates_subset.rb', line 35

def validate_each(record, attribute, value)
  add_errors_or_raise(options, record, attribute) unless is_subset?(value, options[:superset])
end