Class: ActiveModel::Validations::ExistenceValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::ExistenceValidator
- Defined in:
- lib/dekiru/validators/existence.rb
Instance Method Summary collapse
Instance Method Details
#exists?(record, value) ⇒ Boolean
16 17 18 19 20 21 22 |
# File 'lib/dekiru/validators/existence.rb', line 16 def exists?(record, value) unless [:in].respond_to?(:call) raise ArgumentError, '`in` option should be proc' end collection = record.instance_exec(&[: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, .except(:in).merge!(value: value)) end end |