Class: ActiveModel::Validations::SubsetValidator

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SubsetValidator



13
14
15
16
17
18
19
20
21
# File 'lib/validates_subset.rb', line 13

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

  merged_options = {
    :message => "is expected to be a subset of #{ options[:superset] } and is not."
  }.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



30
31
32
# File 'lib/validates_subset.rb', line 30

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