Class: ActiveModel::Validations::ExistenceValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/dekiru/validators/existence.rb

Instance Method Summary collapse

Instance Method Details

#exists?(record, value) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/dekiru/validators/existence.rb', line 18

def exists?(record, value)
  unless options[:in].respond_to?(:call)
    raise ArgumentError, '`in` option should be proc'
  end
  collection = record.instance_exec(&options[:in])
  collection.exists?(value)
end

#validate_each(record, attribute, value) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/dekiru/validators/existence.rb', line 10

def validate_each(record, attribute, value)
  ActiveSupport::Deprecation.warn('`ExistenceValidator` is deprecated and will be removed in v0.5. Please use `inclusion: { in: xx }` instead.')

  unless exists?(record, value)
    record.errors.add(attribute, :existence, **options.except(:in).merge!(value: value))
  end
end