CustomErrors Plugin

Hi All,

I just wanted to get some feedback on a plugin that I’ve extracted from
a
project that I’m working on. It’s my first plugin so please be gentle.

I tried the error_messages_for plugin. This gives an excellent level of
customization, but I only needed fairly simple mods to the standard
error
messages. I also didn’t want to specify all the niceness on each call
to
error_messages_for with a big hash that I would have to repeat in the
different places that I call it.

With all that out of the way, I have attached the plugin to this post
because I don’t have anywhere to store it online.

The readme is listed here:

CustomErrors V0.1

Welcome to the CustomErrors plugin for Rails. This plugin aims to make
customizations to the returned error messages from the
error_messages_for
method simple and DRY.

USAGE

To customize the error messages you can use any, some or none of the
following options.

In The Model

In your model include a constant declaration
NICE_ATTR_NAMES = { :some_attr => “A Nice Name”,
:another => “Another Nice Name” }

This in addition to the :message option in the validates*** call will
provide a good level of custom error messages for the error on that
particular attribute.

This constant does not need to include nice names for every attribute.
If
the attribute is not declared in the NICE_ATTR_NAMES constant, the
plugin
will fall back on the original format.

Options In The error_message_for Call

There are a number of options included to extend the customizability of
the
individual call to the error_messages_for in addtition to the standard
options.

nice_name -> Replaces the name of the instance variable in the
heading
of the returned div
nice_header -> Replaces the entire heading of the returned div
sub_header -> Replaces the entire sub-heading of the returned div

Example

(The class definition may not be complete. This is only to show the
concept.)

class Toy < ActiveRecord::Base
belongs_to :user

validates_presence_of :user, :message => ‘is not much good without
someone
to play with it!’
validates_length_of :name, :minimum => 3

NICE_ATTR_NAMES = { :user => “A Toy” }

end

Say we then try to save the toy with no user and the toy name of only 2
chars and a toy_type attribute assumed to be set to “Transformer”. Two
examples of a call follow:

error_messaages_for( ‘instance_of_toy’,
:nice_name => “#{@instance_of_toy.toy_type}”)

results in
h2. 2 errors prohibited this Transformer from being saved
sub. There were problems with the following fields:
list. * A Toy is not much good without someone to play with it!
* Name is too short (minimum is 3 characters)

Again the same state of the toy instance variable will be assumed with
the
following call

error_messages_for( ‘instance_of_toy’,
:nice_header => “Oops! You need to fix the
#{pluralize(@instance_of_toy.errors.count, ‘error’, ‘errors’) before we
can
save this #{@instance_of_toy.toy_type}”,
:sub_header => “The kids won’t be happy until all
these
are fixed:” )

results in
h2. Oops! You need to fix the 2 errors before we can save this
Transformer
sub. The kids won’t be happy until all these are fixed:
list. * A Toy is not much good without someone to play with it!
* Name is too short (minimum is 3 characters)

Tests

I have not written any tests for this yet… I know, but I’m not very
confident with them yet.

Feel free to send bug reports/fixes to me at [email protected]