Class: ActiveModelValidatesIntersectionOf::Validator

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

Constant Summary collapse

ERROR_MESSAGE =
"An object with the method #include? or a proc, lambda or symbol is required, " \
"and must be supplied as the :in (or :within) option"

Instance Method Summary collapse

Instance Method Details

#check_validity!Object



6
7
8
9
10
# File 'lib/active_model_validates_intersection_of/validator.rb', line 6

def check_validity!
  unless delimiter.respond_to?(:include?) || delimiter.respond_to?(:call) || delimiter.respond_to?(:to_sym)
    raise ArgumentError, ERROR_MESSAGE
  end
end

#validate_each(record, attribute, value) ⇒ Object

Raises:

  • (ArgumentError)


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

def validate_each(record, attribute, value)
  raise ArgumentError, "value must be an array" unless value.is_a?(Array)
  
  if (value - members(record)).size > 0
    record.errors.add(attribute, :inclusion, **options.except(:in, :within).merge!(value: value))
  end
end