From a152e5c73204cfd0cdf1abf60f1b1142402b9c8e Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Sat, 10 Apr 2010 00:48:24 +1000 Subject: [PATCH] Re-define empty? for errors to check if the values inside the OrderedHash are empty rather than the OrderedHash itself. --- activemodel/lib/active_model/errors.rb | 5 +++++ activemodel/test/cases/validations_test.rb | 6 ++++++ 2 files changed, 11 insertions(+), 0 deletions(-) diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 64b28f6..e6c86c7 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -142,6 +142,11 @@ module ActiveModel to_a.size end + # Returns true if there are any errors, false if not. + def empty? + all? { |k, v| v && v.empty? } + end + # Returns an xml formatted representation of the Errors hash. # # p.errors.add(:name, "can't be blank") diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 9fedd84..087114c 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -97,6 +97,12 @@ class ValidationsTest < ActiveModel::TestCase assert_equal 2, r.errors.count end + def test_errors_empty_after_errors_on_check + t = Topic.new + t.errors.on(:id) + assert t.errors.empty? + end + def test_validates_each hits = 0 Topic.validates_each(:title, :content, [:title, :content]) do |record, attr| -- 1.6.0.4