From 7c4681940763e430fc80dd3ecd71404450beb23b Mon Sep 17 00:00:00 2001 From: Lawrence Pit Date: Fri, 11 Sep 2009 16:18:34 +1000 Subject: [PATCH] Added errors_on and Errors.error methods --- activerecord/lib/active_record/validations.rb | 19 ++++++++++++++- activerecord/test/cases/validations_test.rb | 31 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 1dc7c96..f2e024b 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -218,9 +218,18 @@ module ActiveRecord errors = @errors[attribute].map(&:to_s) errors.size == 1 ? errors.first : errors end - alias :[] :on + # Returns +nil+, if no errors are associated with the specified +attribute+. + # Returns the error object, if one error is associated with the specified +attribute+. + # Returns an array of error objects, if more than one error is associated with the specified +attribute+. + def error(attribute) + attribute = attribute.to_s + return nil unless @errors.has_key?(attribute) + errors = @errors[attribute] + errors.size == 1 ? errors.first : errors + end + # Returns errors assigned to the base object through +add_to_base+ according to the normal rules of on(attribute). def on_base on(:base) @@ -1122,6 +1131,14 @@ module ActiveRecord @errors ||= Errors.new(self) end + # Returns +nil+, if no errors are associated with the specified +attribute+. + # Returns the error object, if one error is associated with the specified +attribute+. + # Returns an array of error objects, if more than one error is associated with the specified +attribute+. + def errors_on(attribute) + errors.error(attribute) + end + alias_method :error_on, :errors_on + protected # Overwrite this method for validation checks on all saves and use Errors.add(field, msg) for invalid attributes. def validate diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 4c023c3..1fdad28 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -92,6 +92,37 @@ class ValidationsTest < ActiveRecord::TestCase assert_equal 2, r.errors.count end + def test_single_attr_double_error_msg + r = Reply.new + r.errors.add("title", "Foo") + r.errors.add("title", "Bar") + assert_equal 2, r.errors[:title].length + assert_equal 2, r.errors.on(:title).length + assert_equal "Foo", r.errors[:title][0] + assert_equal "Bar", r.errors[:title][1] + end + + def test_single_attr_double_error_object + r = Reply.new + r.errors.add("title", "Foo") + r.errors.add("title", "Bar") + assert_equal 2, r.errors_on(:title).length + assert_equal "Foo", r.errors_on(:title)[0].message + assert_equal "Bar", r.errors_on(:title)[1].message + end + + def test_single_attr_single_error_object + r = Reply.new + r.errors.add("title", "Foo") + assert_equal "Foo", r.errors_on(:title).message + end + + def test_errors_on_aliased_with_error_on + r = Reply.new + r.errors.add("title", "Foo") + assert_equal "Foo", r.error_on(:title).message + end + def test_error_on_create r = Reply.new r.title = "Wrong Create" -- 1.5.5.1