Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8139 (new defect)

Opened 1 year ago

Last modified 10 months ago

[PATCH] Improve validations errors system

Reported by: divoxx Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

Current activerecord validation implementation store string messages directly into the Error object. This approach is simple and works, but it has some down sides.

Supposing you're testing your application and you want to assert against a specific error in the object. You'd usually do this:

assert_equal "can't be empty", errors.on(:some_attr)

By using the string message in comparison you're accepting the fact that if your message string change your test will be breaking. It would be easier to do:

assert_equal :empty, errors.on(:some_attr)

Where :empty is a identifier for the error message stored in ActiveRecord::Errors.default_error_messages

This is fully backward compatible, so if you have comparisons against string messages, it will also work.

There's a little difference in ActiveRecord::Errors#each. Instead of yielding the attribute and the message string, it will yield the attribute and a instance of ActiveRecord::Errors::ErrorMessage. But since ErroMessage#to_s will return the string message, you should be fine ;-)

I's pretty much only the internals that has changed and still have the same behaviour, so there're only few tests included. The current tests cover all the common behaviour.

Attachments

active_record_identified_validations.6545.diff (14.1 kB) - added by divoxx on 04/21/07 13:15:17.

Change History

04/21/07 13:14:37 changed by divoxx

  • summary changed from [PATCH] Improve how validations errors system to [PATCH] Improve validations errors system.

I forgot to mention that this patch depends on other patch that lives in Ticket #8138

04/21/07 13:15:17 changed by divoxx

  • attachment active_record_identified_validations.6545.diff added.

04/21/07 13:28:51 changed by divoxx

I just realize that i forgot to patch the rdoc comments. If this patch is of core's interest, tell me and I'll improve the patch to cover the documentation.

12/30/07 02:31:46 changed by arthurgeek

+1 on this one.

I think strings shouldn't be returned on ErrorsObject directly. This should be available only at the view, and not deal with those strings directly on Object.