From e714b499cc1f7ebc84f8d0e96607b79e60f2828d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 20 Oct 2009 22:20:01 -0200 Subject: Move validator, human_name and human_attribute_name to ActiveModel, remove deprecated error messages and add i18n_scope and lookup_ancestors. Signed-off-by: Carl Lerche --- activemodel/test/cases/naming_test.rb | 2 +- activemodel/test/cases/translation_test.rb | 51 ++++++++++++++++++++++ .../i18n_generate_message_validation_test.rb | 1 - .../test/cases/validations/i18n_validation_test.rb | 8 ++++ .../test/cases/validations/with_validation_test.rb | 8 ++-- 5 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 activemodel/test/cases/translation_test.rb (limited to 'activemodel/test') diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index 4d97af3d13..fe1ea36450 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -2,7 +2,7 @@ require 'cases/helper' class NamingTest < ActiveModel::TestCase def setup - @model_name = ActiveModel::Name.new('Post::TrackBack') + @model_name = ActiveModel::Name.new(self, 'Post::TrackBack') end def test_singular diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb new file mode 100644 index 0000000000..d171784963 --- /dev/null +++ b/activemodel/test/cases/translation_test.rb @@ -0,0 +1,51 @@ +require 'cases/helper' + +class SuperUser + extend ActiveModel::Translation +end + +class User < SuperUser +end + +class ActiveModelI18nTests < ActiveModel::TestCase + + def setup + I18n.backend = I18n::Backend::Simple.new + end + + def test_translated_model_attributes + I18n.backend.store_translations 'en', :activemodel => {:attributes => {:super_user => {:name => 'super_user name attribute'} } } + assert_equal 'super_user name attribute', SuperUser.human_attribute_name('name') + end + + def test_translated_model_attributes_with_symbols + I18n.backend.store_translations 'en', :activemodel => {:attributes => {:super_user => {:name => 'super_user name attribute'} } } + assert_equal 'super_user name attribute', SuperUser.human_attribute_name(:name) + end + + def test_translated_model_attributes_with_ancestor + I18n.backend.store_translations 'en', :activemodel => {:attributes => {:user => {:name => 'user name attribute'} } } + assert_equal 'user name attribute', User.human_attribute_name('name') + end + + def test_translated_model_attributes_with_ancestors_fallback + I18n.backend.store_translations 'en', :activemodel => {:attributes => {:super_user => {:name => 'super_user name attribute'} } } + assert_equal 'super_user name attribute', User.human_attribute_name('name') + end + + def test_translated_model_names + I18n.backend.store_translations 'en', :activemodel => {:models => {:super_user => 'super_user model'} } + assert_equal 'super_user model', SuperUser.model_name.human + end + + def test_translated_model_names_with_sti + I18n.backend.store_translations 'en', :activemodel => {:models => {:user => 'user model'} } + assert_equal 'user model', User.model_name.human + end + + def test_translated_model_names_with_ancestors_fallback + I18n.backend.store_translations 'en', :activemodel => {:models => {:super_user => 'super_user model'} } + assert_equal 'super_user model', User.model_name.human + end +end + diff --git a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb index 443a81c6ac..54b2405c92 100644 --- a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb @@ -63,7 +63,6 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase assert_equal 'custom message title', @person.errors.generate_message(:title, :exclusion, :default => 'custom message {{value}}', :value => 'title') end - # validates_associated: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value) # validates_format_of: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value) def test_generate_message_invalid_with_default_message assert_equal 'is invalid', @person.errors.generate_message(:title, :invalid, :default => nil, :value => 'title') diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index fc4f1926b0..68b1b27f75 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -56,6 +56,12 @@ class I18nValidationTest < ActiveModel::TestCase @person.errors.add_on_blank :title, 'custom' end + def test_errors_full_messages_translates_human_attribute_name_for_model_attributes + @person.errors.add('name', 'empty') + I18n.expects(:translate).with(:"person.name", :default => ['Name'], :scope => [:activemodel, :attributes], :count => 1).returns('Name') + @person.errors.full_messages + end + # ActiveRecord::Validations # validates_confirmation_of w/ mocha def test_validates_confirmation_of_generates_message @@ -494,6 +500,8 @@ class I18nValidationTest < ActiveModel::TestCase assert_equal ['global message'], @person.errors[:title] end + # test with validates_with + def test_validations_with_message_symbol_must_translate I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:custom_error => "I am a custom error"}}} Person.validates_presence_of :title, :message => :custom_error diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index c290b49a28..fae87a6188 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -13,24 +13,24 @@ class ValidatesWithTest < ActiveRecord::TestCase ERROR_MESSAGE = "Validation error from validator" OTHER_ERROR_MESSAGE = "Validation error from other validator" - class ValidatorThatAddsErrors < ActiveRecord::Validator + class ValidatorThatAddsErrors < ActiveModel::Validator def validate() record.errors[:base] << ERROR_MESSAGE end end - class OtherValidatorThatAddsErrors < ActiveRecord::Validator + class OtherValidatorThatAddsErrors < ActiveModel::Validator def validate() record.errors[:base] << OTHER_ERROR_MESSAGE end end - class ValidatorThatDoesNotAddErrors < ActiveRecord::Validator + class ValidatorThatDoesNotAddErrors < ActiveModel::Validator def validate() end end - class ValidatorThatValidatesOptions < ActiveRecord::Validator + class ValidatorThatValidatesOptions < ActiveModel::Validator def validate() if options[:field] == :first_name record.errors[:base] << ERROR_MESSAGE -- cgit v1.2.3