← Back to Upcase

Refactoring - Extract Validator

(Upcase ) #1

This topic is for the [Extract Validator][] exercise in the [Refactoring][] trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
[Extract Validator]: https://exercises.upcase.com/exercises/extract-validator
[Refactoring]: https://thoughtbot.com/upcase/refactoring

(Rick Liu) #2

What’s the difference between ActiveModel::Validator and ActiveModel::EachValidator ?
We can use both of them to implement this custom validator.

(Chris Toomey) #3

Hi @rckv, the difference is that ActiveModel::Validator subclasses will have a validate method that takes a whole record, while ActiveModel::EachValidator will have a validate_each method which takes all of record, attribute, value as params. The EachValidator is described as:

The easiest way to add custom validators for validating individual attributes is with the convenient ActiveModel::EachValidator

You can read more here: http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations