Forum: Rails-core (closed, excessive spam) Validation levels: errors, warnings

Bce1d1b7c3ec7b577dcb42e254899e6b?d=identicon&s=25 Michael Schuerig (Guest)
on 2008-05-03 01:23
(Received via mailing list)
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

--
Michael Schuerig
mailto:michael@schuerig.de
http://www.schuerig.de/michael/
Efa76b164a7de4a5730e4fa397cc4425?d=identicon&s=25 Michael Koziarski (Guest)
on 2008-05-04 03:54
(Received via mailing list)
>  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.

--
Cheers

Koz
Bce1d1b7c3ec7b577dcb42e254899e6b?d=identicon&s=25 Michael Schuerig (Guest)
on 2008-05-04 09:29
(Received via mailing list)
On Sunday 04 May 2008, Michael Koziarski wrote:
> validations.
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

--
Michael Schuerig
mailto:michael@schuerig.de
http://www.schuerig.de/michael/
Efa76b164a7de4a5730e4fa397cc4425?d=identicon&s=25 Michael Koziarski (Guest)
on 2008-05-04 10:41
(Received via mailing list)
>  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.




--
Cheers

Koz
Bce1d1b7c3ec7b577dcb42e254899e6b?d=identicon&s=25 Michael Schuerig (Guest)
on 2008-05-04 13:04
(Received via mailing list)
On Sunday 04 May 2008, Michael Koziarski wrote:
> relatively low impact perhaps it's not so bad.
http://rails.lighthouseapp.com/projects/8994-ruby-...

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

Michael

--
Michael Schuerig
mailto:michael@schuerig.de
http://www.schuerig.de/michael/
This topic is locked and can not be replied to.