Permalink
Browse files

Added add_to_base and on_base methods to Validatable::Errors (joseico…

…sta)
  • Loading branch information...
1 parent 0158007 commit 938e14989f0d0b20a2637203a11aa179118bf421 @jnunemaker committed Apr 18, 2010
Showing with 19 additions and 0 deletions.
  1. +13 −0 lib/validatable/errors.rb
  2. +6 −0 test/unit/test_errors.rb
View
@@ -19,6 +19,19 @@ def invalid?(attribute)
!@errors[attribute.to_sym].nil?
end
+ # Adds an error to the base object instead of any particular attribute. This is used
+ # to report errors that don't tie to any specific attribute, but rather to the object
+ # as a whole. These error messages don't get prepended with any field name when iterating
+ # with +each_full+, so they should be complete sentences.
+ def add_to_base(msg)
+ add(:base, msg)
+ end
+
+ # Returns errors assigned to the base object through +add_to_base+ according to the normal rules of <tt>on(attribute)</tt>.
+ def on_base
+ on(:base)
+ end
+
# call-seq: on(attribute)
#
# * Returns nil, if no errors are associated with the specified +attribute+.
View
@@ -7,6 +7,12 @@
errors.on(:attribute)
end
+ expect "message" do
+ errors = Validatable::Errors.new
+ errors.add_to_base("message")
+ errors.on_base
+ end
+
expect ["message"] do
errors = Validatable::Errors.new
errors.add(:attribute, "message")

0 comments on commit 938e149

Please sign in to comment.