Validation levels: errors, warnings

Normal validation messages tell the user that something is so terribly
wrong as to prohibit an object from saving. I recently had the
additional need to warn users that somethings fishy about their object
even though it doesn't preclude saving it.

In that particular case fishiness only involved a custom validation
method and a new 'warning' level

  def validates_not_fishy(options = {})
    level = options[:level] ?
      options[:level].to_s.pluralize.to_sym :
      :errors
    send(validation_method(options[:on] || :save)) do |record|
      if something_is_fishy
        # Instead of
        # record.errors.add_to_base("Something is wrong")
        # do
        record.send(level).add_to_base("Something is wrong")
      end
    end
  end

  def warnings
    @warnings ||= ActiveRecord::Errors.new(self)
  end

All of the above can, of course, be easily added to the classes where it
is needed, or duck punched into ActiveRecord. What I'd like to have is
a way to use the already existing validations for custom validation
levels, too. To be clear, I don't want any further levels to be added
to AR, but I want the necessary hooks to add them myself in an app or
plugin.

Michael

All of the above can, of course, be easily added to the classes where it
is needed, or duck punched into ActiveRecord. What I'd like to have is
a way to use the already existing validations for custom validation
levels, too. To be clear, I don't want any further levels to be added
to AR, but I want the necessary hooks to add them myself in an app or
plugin.

What kind of hooks do you have in mind? I'm always a little suspect
of adding 'hooks' for some theoretical future extensibility, but
there's definitely some scope for improving the implementation of the
validations.

I've always found it a little frustrating that the actual logic for
'validating the presence of' isn't encapsulated in a single function
that you can call from your own validations. So the validations could
be used in situations where the macros don't apply.

I'm asking for nothing more than

  record.send(level).add_to_base("Something is wrong")

instead of

  record.errors.add_to_base("Something is wrong")

wherever it applies with level defaulting to :errors, i.e.

  level = options[:level] ?
options[:level].to_s.pluralize.to_sym :
:errors

Michael

wherever it applies with level defaulting to :errors, i.e.

  level = options[:level] ?
     options[:level].to_s.pluralize.to_sym :
     :errors

Whip up a patch for this so we can see how widespread the result is.
I'm a little suspect of making this kind of change, but if it's
relatively low impact perhaps it's not so bad.

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/107-level-hooks-for-validations

I have used neither lighthouse nor git before, I hope I didn't blow
anything.

Michael