From 1cffc6efca234154b632b079cd7524af90d64015 Mon Sep 17 00:00:00 2001 From: pivotal Date: Mon, 30 Nov 2009 13:33:49 -0800 Subject: [PATCH] added attribute option to #full_messages to return messages specific to that attribute --- activerecord/lib/active_record/validations.rb | 10 ++++- activerecord/test/cases/errors_test.rb | 61 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletions(-) create mode 100644 activerecord/test/cases/errors_test.rb diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 568e530..6c66d63 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -271,10 +271,18 @@ module ActiveRecord # company.errors.full_messages # => # ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"] def full_messages(options = {}) - @errors.values.inject([]) do |full_messages, errors| + errors_of_interest(options).inject([]) do |full_messages, errors| full_messages + errors.map { |error| error.full_message } end end + + def errors_of_interest(options) + if options[:attribute] + [@errors[options[:attribute].to_s].to_a] + else + @errors.values + end + end # Returns true if no errors have been added. def empty? diff --git a/activerecord/test/cases/errors_test.rb b/activerecord/test/cases/errors_test.rb new file mode 100644 index 0000000..db7d894 --- /dev/null +++ b/activerecord/test/cases/errors_test.rb @@ -0,0 +1,61 @@ +# encoding: utf-8 +require 'cases/helper' +require 'models/topic' + +class ErrorsTest < ActiveRecord::TestCase + repair_validations(Topic) + + def test_full_messages_with_attribute_option_returns_error_strings_for_that_attribute + Topic.validates_presence_of(:title) + topic = Topic.create("title" => "") + assert !topic.valid? + + messages = topic.errors.full_messages(:attribute => :title) + assert messages.include?("Title can't be blank") + end + + def test_full_messages_with_string_attribute_option_returns_error_strings_for_that_attribute + Topic.validates_presence_of(:title) + topic = Topic.create("title" => "") + assert !topic.valid? + + messages = topic.errors.full_messages('attribute' => 'title') + assert messages.include?("Title can't be blank") + end + + def test_full_messages_with_attribute_option_does_not_return_error_strings_for_other_attributes + Topic.validates_presence_of(:title) + topic = Topic.create("title" => "") + assert !topic.valid? + + messages = topic.errors.full_messages(:attribute => :author_name) + assert messages.empty? + end + + def test_full_messages_without_attribute_option_returns_all_error_strings + Topic.validates_presence_of(:title) + Topic.validates_presence_of(:author_name) + topic = Topic.create("title" => "", "author_name" => "") + assert !topic.valid? + + messages = topic.errors.full_messages + assert messages.include?("Title can't be blank") + assert messages.include?("Author name can't be blank") + end + + def test_full_messages_with_attribute_option_for_attribute_with_no_validation_error_returns_empty_array + topic = Topic.create + assert topic.valid? + + messages = topic.errors.full_messages(:attribute => :title) + assert messages.empty? + end + + def test_full_messages_with_nonexistant_attribute_option_returns_empty_array + topic = Topic.create + assert topic.valid? + + messages = topic.errors.full_messages(:attribute => :foobar) + assert messages.empty? + end +end -- 1.6.3.3