Looking at the rails source for ActiveModel::Validations, the HelperMethods module is both included and extended in the underlying model:
module Validations
extend ActiveSupport::Concern
included do
extend ActiveModel::Callbacks
extend ActiveModel::Translation
extend HelperMethods
include HelperMethods
...
Theoretically, this allows you to not only call helper methods like validates_presence_of
as class macros (as shown in all the standard examples), but to also call these helper methods directly on the model instance:
myobject.validates_presence_of :name
Where I can see this being useful is in the context of a custom validator that wants to leverage some of the existing helper methods:
class CustomValidator < ActiveModel::Validator
def validate(record)
record.validates_presence_of :name
record.validates_acceptance_of :terms
...
end
end
And whether or not this is encouraged behavior, it did seem to work until some changes with specific validators in Rails 4.1. So my question is if rails does not support calling these helper methods in this fashion (as instance methods on the model), why are they included in the model instead of just extended?