Class: ActiveModel::Validations::InclusionValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/caring_form/active_model/monkey_patching.rb

Overview

backport inclusion validation from v3.1+ where :in can be a lambda/proc

Constant Summary collapse

ERROR_MESSAGE =
"An object with the method #include? or a proc or lambda is required, " <<
"and must be supplied as the :in option of the configuration hash"

Instance Method Summary collapse

Instance Method Details

#check_validity!Object



12
13
14
15
16
# File 'lib/caring_form/active_model/monkey_patching.rb', line 12

def check_validity!
  unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) }
    raise ArgumentError, ERROR_MESSAGE
  end
end

#validate_each(record, attribute, value) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/caring_form/active_model/monkey_patching.rb', line 18

def validate_each(record, attribute, value)
  delimiter = options[:in]
  exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter
  unless exclusions.send(inclusion_method(exclusions), value)
    record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value))
  end
end