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)


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

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
# File 'lib/dekiru/validators/existence.rb', line 10

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