Skip to content

Commit

Permalink
Fix error_messages_for i18n issue if object_name has underscores [#3629
Browse files Browse the repository at this point in the history
… status:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
m4n authored and josevalim committed Feb 17, 2010
1 parent 4158282 commit 6227ec1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/helpers/active_record_helper.rb
Expand Up @@ -193,8 +193,8 @@ def error_messages_for(*params)
header_message = if options.include?(:header_message)
options[:header_message]
else
object_name = options[:object_name].to_s.gsub('_', ' ')
object_name = I18n.t(object_name, :default => object_name, :scope => [:activerecord, :models], :count => 1)
object_name = options[:object_name].to_s
object_name = I18n.t(object_name, :default => object_name.gsub('_', ' '), :scope => [:activerecord, :models], :count => 1)
locale.t :header, :count => count, :model => object_name
end
message = options.include?(:message) ? options[:message] : locale.t(:body)
Expand Down
11 changes: 9 additions & 2 deletions actionpack/test/template/active_record_helper_i18n_test.rb
Expand Up @@ -2,7 +2,7 @@

class ActiveRecordHelperI18nTest < Test::Unit::TestCase
include ActionView::Helpers::ActiveRecordHelper

attr_reader :request
def setup
@object = stub :errors => stub(:count => 1, :full_messages => ['full_messages'])
Expand Down Expand Up @@ -35,10 +35,17 @@ def test_error_messages_for_given_no_message_option_it_translates_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_object_name_it_translates_object_name
I18n.expects(:t).with(:header, :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => @object_name).returns "1 error prohibited this #{@object_name} from being saved"
I18n.expects(:t).with(@object_name, :default => @object_name, :count => 1, :scope => [:activerecord, :models]).once.returns @object_name
error_messages_for(:object => @object, :locale => 'en', :object_name => @object_name)
end

def test_error_messages_for_given_object_name_with_underscore_it_translates_object_name
I18n.expects(:t).with('bank_account', :default => 'bank account', :count => 1, :scope => [:activerecord, :models]).once.returns 'bank account'
I18n.expects(:t).with(:header, :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => 'bank account').returns "1 error prohibited this bank account from being saved"
error_messages_for(:object => @object, :locale => 'en', :object_name => 'bank_account')
end
end

0 comments on commit 6227ec1

Please sign in to comment.