Reading the documentation of rails about custom validation I see 3 way of doing this :
- Custom validator inheriting from
ActiveModel::Validator
-> prototype :def validate(record)
we use it withvalidate_with
- Custom validator inheriting from
ActiveModel::EachValidator
-> prototypedef validate_each(record, attribute, value)
, we use it when we dovalidates :email, unique: true
e.g - Custom methods - no parameters
valide :name_of_method
Now what I'd like to understand is: Why is there 3 choices of accomplish this ? Which scenario make you choose which options ? I mean we probably can code everything with only one of the option. Just curious about the use-case you're using one of the option in. Are you following any convention ?
Edit : I believe I wasn't clear enough, I am looking for real and concrete example about the usage of those custom validations, which kind of design could involve having ActiveModel::Validator
e.g ? On my end I found this afternoon a pretty good usage for custom methods :
validate :validate_client_id_not_changed, on: :update
def validate_client_id_not_changed
errors.add :client_id, :changed if client_id_changed?
end
I am looking for concrete example running on production, like the example above, not generic one.