Method: ActiveModel::Validations::ClassMethods#validates_with
- Defined in:
- lib/active_model/validations/with.rb
#validates_with(*args, &block) ⇒ Object
If you pass any additional configuration options, they will be passed to the class and available as options:
class Person
include ActiveModel::Validations
validates_with MyValidator, :my_custom_key => "my custom value"
end
class MyValidator < ActiveModel::Validator
def validate(record)
[:my_custom_key] # => "my custom value"
end
end
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_model/validations/with.rb', line 79 def validates_with(*args, &block) = args. args.each do |klass| validator = klass.new(, &block) validator.setup(self) if validator.respond_to?(:setup) if validator.respond_to?(:attributes) && !validator.attributes.empty? validator.attributes.each do |attribute| _validators[attribute.to_sym] << validator end else _validators[nil] << validator end validate(validator, ) end end |