mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Bring agnosticism to error_messages_for.
This commit is contained in:
@@ -202,8 +202,8 @@ module ActionView
|
||||
end
|
||||
|
||||
objects.compact!
|
||||
|
||||
count = objects.inject(0) {|sum, object| sum + object.errors.count }
|
||||
|
||||
unless count.zero?
|
||||
html = {}
|
||||
[:id, :class].each do |key|
|
||||
@@ -216,16 +216,20 @@ module ActionView
|
||||
end
|
||||
options[:object_name] ||= params.first
|
||||
|
||||
I18n.with_options :locale => options[:locale], :scope => [:activerecord, :errors, :template] do |locale|
|
||||
I18n.with_options :locale => options[:locale], :scope => [:activemodel, :errors, :template] do |locale|
|
||||
header_message = if options.include?(:header_message)
|
||||
options[:header_message]
|
||||
else
|
||||
object_name = options[:object_name].to_s.gsub('_', ' ')
|
||||
object_name = I18n.t(options[:object_name].to_s, :default => object_name, :scope => [:activerecord, :models], :count => 1)
|
||||
locale.t :header, :count => count, :model => object_name
|
||||
locale.t :header, :count => count, :model => options[:object_name].to_s.gsub('_', ' ')
|
||||
end
|
||||
|
||||
message = options.include?(:message) ? options[:message] : locale.t(:body)
|
||||
error_messages = objects.sum {|object| object.errors.full_messages.map {|msg| content_tag(:li, ERB::Util.html_escape(msg)) } }.join
|
||||
|
||||
error_messages = objects.sum do |object|
|
||||
object.errors.full_messages.map do |msg|
|
||||
content_tag(:li, ERB::Util.html_escape(msg))
|
||||
end
|
||||
end.join
|
||||
|
||||
contents = ''
|
||||
contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
|
||||
|
||||
@@ -102,7 +102,7 @@
|
||||
minute: "Minute"
|
||||
second: "Seconds"
|
||||
|
||||
activerecord:
|
||||
activemodel:
|
||||
errors:
|
||||
template:
|
||||
header:
|
||||
@@ -114,4 +114,4 @@
|
||||
support:
|
||||
select:
|
||||
# default value for :prompt => true in FormOptionsHelper
|
||||
prompt: "Please select"
|
||||
prompt: "Please select"
|
||||
|
||||
42
actionpack/test/template/active_model_helper_i18n_test.rb
Normal file
42
actionpack/test/template/active_model_helper_i18n_test.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
require 'abstract_unit'
|
||||
|
||||
class ActiveModelHelperI18nTest < Test::Unit::TestCase
|
||||
include ActionView::Context
|
||||
include ActionView::Helpers::ActiveModelHelper
|
||||
|
||||
attr_reader :request
|
||||
|
||||
def setup
|
||||
@object = stub :errors => stub(:count => 1, :full_messages => ['full_messages'])
|
||||
@object.stubs :to_model => @object
|
||||
@object.stubs :class => stub(:model_name => stub(:human => ""))
|
||||
|
||||
@object_name = 'book_seller'
|
||||
@object_name_without_underscore = 'book seller'
|
||||
|
||||
stubs(:content_tag).returns 'content_tag'
|
||||
|
||||
I18n.stubs(:t).with(:'header', :locale => 'en', :scope => [:activemodel, :errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved"
|
||||
I18n.stubs(:t).with(:'body', :locale => 'en', :scope => [:activemodel, :errors, :template]).returns 'There were problems with the following fields:'
|
||||
end
|
||||
|
||||
def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message
|
||||
I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:activemodel, :errors, :template], :count => 1, :model => '').never
|
||||
error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en')
|
||||
end
|
||||
|
||||
def test_error_messages_for_given_no_header_option_it_translates_header_message
|
||||
I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:activemodel, :errors, :template], :count => 1, :model => '').returns 'header message'
|
||||
error_messages_for(:object => @object, :locale => 'en')
|
||||
end
|
||||
|
||||
def test_error_messages_for_given_a_message_option_it_does_not_translate_message
|
||||
I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activemodel, :errors, :template]).never
|
||||
error_messages_for(:object => @object, :message => 'message', :locale => 'en')
|
||||
end
|
||||
|
||||
def test_error_messages_for_given_no_message_option_it_translates_message
|
||||
I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activemodel, :errors, :template]).returns 'There were problems with the following fields:'
|
||||
error_messages_for(:object => @object, :locale => 'en')
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,6 @@
|
||||
require 'abstract_unit'
|
||||
|
||||
class ActiveRecordHelperTest < ActionView::TestCase
|
||||
class ActiveModelHelperTest < ActionView::TestCase
|
||||
tests ActionView::Helpers::ActiveModelHelper
|
||||
|
||||
silence_warnings do
|
||||
@@ -1,51 +0,0 @@
|
||||
require 'abstract_unit'
|
||||
|
||||
class ActiveRecordHelperI18nTest < Test::Unit::TestCase
|
||||
include ActionView::Context
|
||||
include ActionView::Helpers::ActiveModelHelper
|
||||
|
||||
attr_reader :request
|
||||
|
||||
def setup
|
||||
@object = stub :errors => stub(:count => 1, :full_messages => ['full_messages'])
|
||||
@object.stubs :to_model => @object
|
||||
@object.stubs :class => stub(:model_name => stub(:human => ""))
|
||||
|
||||
@object_name = 'book_seller'
|
||||
@object_name_without_underscore = 'book seller'
|
||||
|
||||
stubs(:content_tag).returns 'content_tag'
|
||||
|
||||
I18n.stubs(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved"
|
||||
I18n.stubs(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:'
|
||||
end
|
||||
|
||||
def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message
|
||||
I18n.expects(:translate).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').never
|
||||
error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en')
|
||||
end
|
||||
|
||||
def test_error_messages_for_given_no_header_option_it_translates_header_message
|
||||
I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns 'header message'
|
||||
I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns ''
|
||||
error_messages_for(:object => @object, :locale => 'en')
|
||||
end
|
||||
|
||||
def test_error_messages_for_given_a_message_option_it_does_not_translate_message
|
||||
I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).never
|
||||
I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns ''
|
||||
error_messages_for(:object => @object, :message => 'message', :locale => 'en')
|
||||
end
|
||||
|
||||
def test_error_messages_for_given_no_message_option_it_translates_message
|
||||
I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:'
|
||||
I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns ''
|
||||
error_messages_for(:object => @object, :locale => 'en')
|
||||
end
|
||||
|
||||
def test_error_messages_for_given_object_name_it_translates_object_name
|
||||
I18n.expects(:t).with(:header, :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => @object_name_without_underscore).returns "1 error prohibited this #{@object_name_without_underscore} from being saved"
|
||||
I18n.expects(:t).with(@object_name, :default => @object_name_without_underscore, :count => 1, :scope => [:activerecord, :models]).once.returns @object_name_without_underscore
|
||||
error_messages_for(:object => @object, :locale => 'en', :object_name => @object_name)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user