From 6d056606c887db42a506349ba8fb99831a266faa Mon Sep 17 00:00:00 2001 From: pivotal Date: Mon, 23 Nov 2009 13:40:55 -0800 Subject: [PATCH] Added attribute option to Errors#full_messages. --- activemodel/lib/active_model/errors.rb | 10 ++++- activemodel/test/cases/errors_test.rb | 63 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 activemodel/test/cases/errors_test.rb diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index e8bb629..c7132d4 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -93,10 +93,10 @@ module ActiveModel # company = Company.create(:address => '123 First St.') # 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 + def full_messages(options = {}) full_messages = [] - each do |attribute, messages| + errors_for(options[:attribute]).each do |attribute, messages| messages = Array.wrap(messages) next if messages.empty? @@ -157,5 +157,11 @@ module ActiveModel I18n.translate(key, options) end + + private + + def errors_for(attribute) + attribute.nil? ? self : { attribute => self.get(attribute.to_sym) } + end end end diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb new file mode 100644 index 0000000..8fd3562 --- /dev/null +++ b/activemodel/test/cases/errors_test.rb @@ -0,0 +1,63 @@ +# encoding: utf-8 +require 'cases/helper' +require 'cases/tests_database' +require 'models/topic' + +class ErrorsTest < ActiveModel::TestCase + include ActiveModel::ValidationsRepairHelper + 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