From 16860ec34f1c516aab0fa2e6701c3ec80456e9bd Mon Sep 17 00:00:00 2001 From: Matjaz Gregoric Date: Fri, 2 Jan 2009 05:20:20 +0100 Subject: [PATCH] Add more flexible formatting for output of ActiveRecord::Errors#full_errors. --- activerecord/lib/active_record/locale/en.yml | 4 +++ activerecord/lib/active_record/validations.rb | 4 ++- activerecord/test/cases/validations_i18n_test.rb | 30 +++++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/locale/en.yml b/activerecord/lib/active_record/locale/en.yml index 7e20543..1c9137b 100644 --- a/activerecord/lib/active_record/locale/en.yml +++ b/activerecord/lib/active_record/locale/en.yml @@ -1,6 +1,10 @@ en: activerecord: errors: + format: + # Used in full_messages, consequently affects the output of error_messages_for helper + # The values :model, :attribute and :message are available for interpolation + full_message: "{{attribute}} {{message}}" # The values :model, :attribute and :value are always available for interpolation # The value :count is available when applicable. Can be used for pluralization. messages: diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 6a9690b..344ba10 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -195,6 +195,7 @@ module ActiveRecord # ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"] def full_messages(options = {}) full_messages = [] + model_name = @base.class.human_name @errors.each_key do |attr| @errors[attr].each do |message| @@ -204,7 +205,8 @@ module ActiveRecord full_messages << message else attr_name = @base.class.human_attribute_name(attr) - full_messages << attr_name + I18n.t('activerecord.errors.format.separator', :default => ' ') + message + full_messages << I18n.translate(:'activerecord.errors.format.full_message', :default => '{{attribute}} {{message}}', + :attribute => attr_name, :message => message, :model => model_name) end end end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index e893a70..df46061 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -162,9 +162,33 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase @topic.errors.add_on_blank :title, 'custom' end - def test_errors_full_messages_translates_human_attribute_name_for_model_attributes - @topic.errors.instance_variable_set :@errors, { 'title' => ['empty'] } - I18n.expects(:translate).with(:"topic.title", :default => ['Title'], :scope => [:activerecord, :attributes], :count => 1).returns('Title') + def test_full_messages_generates_messages_according_to_specified_format + @topic.errors.instance_variable_set :@errors, {'title' => ['empty'] } + + I18n.backend.store_translations 'en', :activerecord => {:errors => {:format => {:full_message => 'Field "{{attribute}}" on "{{model}}": {{message}}'}}} + + I18n.expects(:translate).with( + :"topic", + :default => ['Topic'], + :scope => [:activerecord, :models], + :count => 1 + ).returns('Topic') + + I18n.expects(:translate).with( + :"topic.title", + :default => ['Title'], + :scope => [:activerecord, :attributes], + :count => 1 + ).returns('Title') + + I18n.expects(:translate).with( + :"activerecord.errors.format.full_message", + :default => '{{attribute}} {{message}}', + :model => 'Topic', + :attribute => 'Title', + :message => 'empty' + ).returns('Field "Title" on "Topic": empty') + @topic.errors.full_messages :locale => 'en' end end -- 1.5.6.1.1071.g76fb