Errors messages are now moved from :activerecord.errors to simply :errors on I18n yml files.

This commit is contained in:
José Valim 2010-01-07 01:04:25 +01:00
parent a323b83acf
commit 190ce3ab37
10 changed files with 108 additions and 228 deletions

@ -8,7 +8,7 @@ gem "rails", "3.0.pre", :path => "railties"
end
# AS
gem "i18n", ">= 0.3.0"
gem "i18n", "0.3.3"
# AR
gem "arel", "0.2.pre", :git => "git://github.com/rails/arel.git"

@ -105,8 +105,7 @@ def full_messages
else
attr_name = attribute.to_s.gsub('.', '_').humanize
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
options = { :default => "{{attribute}} {{message}}", :attribute => attr_name,
:scope => @base.class.i18n_scope }
options = { :default => "{{attribute}} {{message}}", :attribute => attr_name }
messages.each do |m|
full_messages << I18n.t(:"errors.format", options.merge(:message => m))
@ -153,7 +152,7 @@ def generate_message(attribute, message = :invalid, options = {})
:model => @base.class.model_name.human,
:attribute => @base.class.human_attribute_name(attribute),
:value => value,
:scope => [@base.class.i18n_scope, :errors]
:scope => [:errors]
}.merge(options)
I18n.translate(key, options)

@ -1,27 +1,26 @@
en:
activemodel:
errors:
# model.errors.full_messages format.
format: "{{attribute}} {{message}}"
errors:
# The default format use in full error messages.
format: "{{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:
inclusion: "is not included in the list"
exclusion: "is reserved"
invalid: "is invalid"
confirmation: "doesn't match confirmation"
accepted: "must be accepted"
empty: "can't be empty"
blank: "can't be blank"
too_long: "is too long (maximum is {{count}} characters)"
too_short: "is too short (minimum is {{count}} characters)"
wrong_length: "is the wrong length (should be {{count}} characters)"
not_a_number: "is not a number"
greater_than: "must be greater than {{count}}"
greater_than_or_equal_to: "must be greater than or equal to {{count}}"
equal_to: "must be equal to {{count}}"
less_than: "must be less than {{count}}"
less_than_or_equal_to: "must be less than or equal to {{count}}"
odd: "must be odd"
even: "must be even"
# The values :model, :attribute and :value are always available for interpolation
# The value :count is available when applicable. Can be used for pluralization.
messages:
inclusion: "is not included in the list"
exclusion: "is reserved"
invalid: "is invalid"
confirmation: "doesn't match confirmation"
accepted: "must be accepted"
empty: "can't be empty"
blank: "can't be blank"
too_long: "is too long (maximum is {{count}} characters)"
too_short: "is too short (minimum is {{count}} characters)"
wrong_length: "is the wrong length (should be {{count}} characters)"
not_a_number: "is not a number"
greater_than: "must be greater than {{count}}"
greater_than_or_equal_to: "must be greater than or equal to {{count}}"
equal_to: "must be equal to {{count}}"
less_than: "must be less than {{count}}"
less_than_or_equal_to: "must be less than or equal to {{count}}"
odd: "must be odd"
even: "must be even"

@ -7,42 +7,6 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase
def setup
Person.reset_callbacks(:validate)
@person = Person.new
@old_load_path, @old_backend = I18n.load_path, I18n.backend
I18n.load_path.clear
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations :'en', {
:activemodel => {
:errors => {
:messages => {
:inclusion => "is not included in the list",
:exclusion => "is reserved",
:invalid => "is invalid",
:confirmation => "doesn't match confirmation",
:accepted => "must be accepted",
:empty => "can't be empty",
:blank => "can't be blank",
:too_long => "is too long (maximum is {{count}} characters)",
:too_short => "is too short (minimum is {{count}} characters)",
:wrong_length => "is the wrong length (should be {{count}} characters)",
:not_a_number => "is not a number",
:greater_than => "must be greater than {{count}}",
:greater_than_or_equal_to => "must be greater than or equal to {{count}}",
:equal_to => "must be equal to {{count}}",
:less_than => "must be less than {{count}}",
:less_than_or_equal_to => "must be less than or equal to {{count}}",
:odd => "must be odd",
:even => "must be even"
}
}
}
}
end
def teardown
I18n.load_path.replace @old_load_path
I18n.backend = @old_backend
end
# validates_inclusion_of: generate_message(attr_name, :inclusion, :default => configuration[:message], :value => value)

@ -12,7 +12,7 @@ def setup
@old_load_path, @old_backend = I18n.load_path, I18n.backend
I18n.load_path.clear
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations('en', :activemodel => {:errors => {:messages => {:custom => nil}}})
I18n.backend.store_translations('en', :errors => {:messages => {:custom => nil}})
end
def teardown
@ -42,13 +42,13 @@ def test_errors_add_on_blank_generates_message_with_custom_default_message
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', 'Name'], :scope => [:activemodel, :attributes], :count => 1).returns('Name')
@person.errors.full_messages
@person.errors.add(:name, 'not found')
Person.expects(:human_attribute_name).with(:name, :default => 'Name').returns("Person's name")
assert_equal ["Person's name not found"], @person.errors.full_messages
end
def test_errors_full_messages_uses_format
I18n.backend.store_translations('en', :activemodel => {:errors => {:format => "Field {{attribute}} {{message}}"}})
I18n.backend.store_translations('en', :errors => {:format => "Field {{attribute}} {{message}}"})
@person.errors.add('name', 'empty')
assert_equal ["Field Name empty"], @person.errors.full_messages
end
@ -254,8 +254,8 @@ def test_validates_numericality_of_less_than_odd_generates_message_with_custom_d
# validates_confirmation_of w/o mocha
def test_validates_confirmation_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:confirmation => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:confirmation => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:confirmation => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:confirmation => 'global message'}}
Person.validates_confirmation_of :title
@person.title_confirmation = 'foo'
@ -264,7 +264,7 @@ def test_validates_confirmation_of_finds_custom_model_key_translation
end
def test_validates_confirmation_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:confirmation => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:confirmation => 'global message'}}
Person.validates_confirmation_of :title
@person.title_confirmation = 'foo'
@ -275,8 +275,8 @@ def test_validates_confirmation_of_finds_global_default_translation
# validates_acceptance_of w/o mocha
def test_validates_acceptance_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:accepted => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:accepted => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:accepted => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:accepted => 'global message'}}
Person.validates_acceptance_of :title, :allow_nil => false
@person.valid?
@ -284,7 +284,7 @@ def test_validates_acceptance_of_finds_custom_model_key_translation
end
def test_validates_acceptance_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:accepted => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:accepted => 'global message'}}
Person.validates_acceptance_of :title, :allow_nil => false
@person.valid?
@ -294,8 +294,8 @@ def test_validates_acceptance_of_finds_global_default_translation
# validates_presence_of w/o mocha
def test_validates_presence_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:blank => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:blank => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:blank => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:blank => 'global message'}}
Person.validates_presence_of :title
@person.valid?
@ -303,7 +303,7 @@ def test_validates_presence_of_finds_custom_model_key_translation
end
def test_validates_presence_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:blank => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:blank => 'global message'}}
Person.validates_presence_of :title
@person.valid?
@ -313,8 +313,8 @@ def test_validates_presence_of_finds_global_default_translation
# validates_length_of :within w/o mocha
def test_validates_length_of_within_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:too_short => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:too_short => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:too_short => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:too_short => 'global message'}}
Person.validates_length_of :title, :within => 3..5
@person.valid?
@ -322,7 +322,7 @@ def test_validates_length_of_within_finds_custom_model_key_translation
end
def test_validates_length_of_within_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:too_short => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:too_short => 'global message'}}
Person.validates_length_of :title, :within => 3..5
@person.valid?
@ -332,8 +332,8 @@ def test_validates_length_of_within_finds_global_default_translation
# validates_length_of :is w/o mocha
def test_validates_length_of_is_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:wrong_length => 'global message'}}
Person.validates_length_of :title, :is => 5
@person.valid?
@ -341,7 +341,7 @@ def test_validates_length_of_is_finds_custom_model_key_translation
end
def test_validates_length_of_is_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:wrong_length => 'global message'}}
Person.validates_length_of :title, :is => 5
@person.valid?
@ -351,8 +351,8 @@ def test_validates_length_of_is_finds_global_default_translation
# validates_format_of w/o mocha
def test_validates_format_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:invalid => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:invalid => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:invalid => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}}
Person.validates_format_of :title, :with => /^[1-9][0-9]*$/
@person.valid?
@ -360,7 +360,7 @@ def test_validates_format_of_finds_custom_model_key_translation
end
def test_validates_format_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:invalid => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}}
Person.validates_format_of :title, :with => /^[1-9][0-9]*$/
@person.valid?
@ -370,8 +370,8 @@ def test_validates_format_of_finds_global_default_translation
# validates_inclusion_of w/o mocha
def test_validates_inclusion_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:inclusion => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:inclusion => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:inclusion => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:inclusion => 'global message'}}
Person.validates_inclusion_of :title, :in => %w(a b c)
@person.valid?
@ -379,7 +379,7 @@ def test_validates_inclusion_of_finds_custom_model_key_translation
end
def test_validates_inclusion_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:inclusion => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:inclusion => 'global message'}}
Person.validates_inclusion_of :title, :in => %w(a b c)
@person.valid?
@ -389,8 +389,8 @@ def test_validates_inclusion_of_finds_global_default_translation
# validates_exclusion_of w/o mocha
def test_validates_exclusion_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:exclusion => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:exclusion => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:exclusion => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:exclusion => 'global message'}}
Person.validates_exclusion_of :title, :in => %w(a b c)
@person.title = 'a'
@ -399,7 +399,7 @@ def test_validates_exclusion_of_finds_custom_model_key_translation
end
def test_validates_exclusion_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:exclusion => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:exclusion => 'global message'}}
Person.validates_exclusion_of :title, :in => %w(a b c)
@person.title = 'a'
@ -410,8 +410,8 @@ def test_validates_exclusion_of_finds_global_default_translation
# validates_numericality_of without :only_integer w/o mocha
def test_validates_numericality_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}}
Person.validates_numericality_of :title
@person.title = 'a'
@ -420,7 +420,7 @@ def test_validates_numericality_of_finds_custom_model_key_translation
end
def test_validates_numericality_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}}
Person.validates_numericality_of :title, :only_integer => true
@person.title = 'a'
@ -431,8 +431,8 @@ def test_validates_numericality_of_finds_global_default_translation
# validates_numericality_of with :only_integer w/o mocha
def test_validates_numericality_of_only_integer_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}}
Person.validates_numericality_of :title, :only_integer => true
@person.title = 'a'
@ -441,7 +441,7 @@ def test_validates_numericality_of_only_integer_finds_custom_model_key_translati
end
def test_validates_numericality_of_only_integer_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}}
Person.validates_numericality_of :title, :only_integer => true
@person.title = 'a'
@ -452,8 +452,8 @@ def test_validates_numericality_of_only_integer_finds_global_default_translation
# validates_numericality_of :odd w/o mocha
def test_validates_numericality_of_odd_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:odd => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:odd => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:odd => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:odd => 'global message'}}
Person.validates_numericality_of :title, :only_integer => true, :odd => true
@person.title = 0
@ -462,7 +462,7 @@ def test_validates_numericality_of_odd_finds_custom_model_key_translation
end
def test_validates_numericality_of_odd_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:odd => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:odd => 'global message'}}
Person.validates_numericality_of :title, :only_integer => true, :odd => true
@person.title = 0
@ -473,8 +473,8 @@ def test_validates_numericality_of_odd_finds_global_default_translation
# validates_numericality_of :less_than w/o mocha
def test_validates_numericality_of_less_than_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:less_than => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:less_than => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:less_than => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:less_than => 'global message'}}
Person.validates_numericality_of :title, :only_integer => true, :less_than => 0
@person.title = 1
@ -483,7 +483,7 @@ def test_validates_numericality_of_less_than_finds_custom_model_key_translation
end
def test_validates_numericality_of_less_than_finds_global_default_translation
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:less_than => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:less_than => 'global message'}}
Person.validates_numericality_of :title, :only_integer => true, :less_than => 0
@person.title = 1
@ -494,7 +494,7 @@ def test_validates_numericality_of_less_than_finds_global_default_translation
# 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"}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:custom_error => "I am a custom error"}}
Person.validates_presence_of :title, :message => :custom_error
@person.title = nil
@person.valid?
@ -502,7 +502,7 @@ def test_validations_with_message_symbol_must_translate
end
def test_validates_with_message_symbol_must_translate_per_attribute
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}
Person.validates_presence_of :title, :message => :custom_error
@person.title = nil
@person.valid?
@ -510,7 +510,7 @@ def test_validates_with_message_symbol_must_translate_per_attribute
end
def test_validates_with_message_symbol_must_translate_per_model
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:custom_error => "I am a custom error"}}}}
I18n.backend.store_translations 'en', :errors => {:models => {:person => {:custom_error => "I am a custom error"}}}
Person.validates_presence_of :title, :message => :custom_error
@person.title = nil
@person.valid?

@ -1,33 +1,9 @@
en:
activerecord:
errors:
# model.errors.full_messages format.
format: "{{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:
inclusion: "is not included in the list"
exclusion: "is reserved"
invalid: "is invalid"
confirmation: "doesn't match confirmation"
accepted: "must be accepted"
empty: "can't be empty"
blank: "can't be blank"
too_long: "is too long (maximum is {{count}} characters)"
too_short: "is too short (minimum is {{count}} characters)"
wrong_length: "is the wrong length (should be {{count}} characters)"
taken: "has already been taken"
not_a_number: "is not a number"
greater_than: "must be greater than {{count}}"
greater_than_or_equal_to: "must be greater than or equal to {{count}}"
equal_to: "must be equal to {{count}}"
less_than: "must be less than {{count}}"
less_than_or_equal_to: "must be less than or equal to {{count}}"
odd: "must be odd"
even: "must be even"
record_invalid: "Validation failed: {{errors}}"
# Append your own errors here or at the model/attributes scope.
errors:
messages:
taken: "has already been taken"
record_invalid: "Validation failed: {{errors}}"
# Append your own errors here or at the model/attributes scope.
# You can define own errors for models or model attributes.
# The values :model, :attribute and :value are always available for interpolation.
@ -42,7 +18,9 @@ en:
# Will define custom blank validation message for User model and
# custom blank validation message for login attribute of User model.
#models:
# ActiveRecord models configuration
#activerecord:
# Translate model names. Used in Model.human_name().
#models:
# For example,
@ -55,4 +33,3 @@ en:
# user:
# login: "Handle"
# will translate User attribute "login" as "Handle"

@ -11,7 +11,7 @@ class RecordInvalid < ActiveRecordError
def initialize(record)
@record = record
errors = @record.errors.full_messages.join(I18n.t('support.array.words_connector', :default => ', '))
super(I18n.t('activerecord.errors.messages.record_invalid', :errors => errors))
super(I18n.t('errors.messages.record_invalid', :errors => errors))
end
end

@ -974,9 +974,9 @@ def test_should_not_use_default_invalid_error_on_associated_models
end
def test_should_default_invalid_error_from_i18n
I18n.backend.store_translations(:en, :activerecord => { :errors => { :models =>
I18n.backend.store_translations(:en, :errors => { :models =>
{ @association_name.to_s.singularize.to_sym => { :blank => "cannot be blank" } }
}})
})
@pirate.send(@association_name).build(:name => '')
@ -985,9 +985,7 @@ def test_should_default_invalid_error_from_i18n
assert_equal ["#{@association_name.to_s.titleize} name cannot be blank"], @pirate.errors.full_messages
assert @pirate.errors[@association_name].empty?
ensure
I18n.backend.store_translations(:en, :activerecord => { :errors => { :models =>
{ @association_name.to_s.singularize.to_sym => nil }
}})
I18n.backend = I18n::Backend::Simple.new
end
def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid

@ -6,15 +6,7 @@ class I18nGenerateMessageValidationTest < ActiveRecord::TestCase
def setup
Topic.reset_callbacks(:validate)
@topic = Topic.new
I18n.backend.store_translations :'en', {
:activerecord => {
:errors => {
:messages => {
:taken => "has already been taken",
}
}
}
}
I18n.backend = I18n::Backend::Simple.new
end
# validates_associated: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value)

@ -4,13 +4,14 @@
class I18nValidationTest < ActiveRecord::TestCase
repair_validations(Topic, Reply)
def setup
Reply.validates_presence_of(:title)
@topic = Topic.new
@old_load_path, @old_backend = I18n.load_path, I18n.backend
I18n.load_path.clear
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations('en', :activerecord => {:errors => {:messages => {:custom => nil}}})
I18n.backend.store_translations('en', :errors => {:messages => {:custom => nil}})
end
def teardown
@ -30,75 +31,6 @@ def replied_topic
end
end
# ActiveRecord::Errors
def test_errors_generate_message_translates_custom_model_attribute_key
I18n.expects(:translate).with(
:topic,
{ :count => 1,
:default => ['Topic'],
:scope => [:activerecord, :models]
}
).returns('Topic')
I18n.expects(:translate).with(
:"topic.title",
{ :count => 1,
:default => ['Title'],
:scope => [:activerecord, :attributes]
}
).returns('Title')
I18n.expects(:translate).with(
:"models.topic.attributes.title.invalid",
:value => nil,
:scope => [:activerecord, :errors],
:default => [
:"models.topic.invalid",
'default from class def error 1',
:"messages.invalid"],
:attribute => "Title",
:model => "Topic"
).returns('default from class def error 1')
@topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1'
end
def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti
I18n.expects(:translate).with(
:reply,
{ :count => 1,
:default => [:topic, 'Reply'],
:scope => [:activerecord, :models]
}
).returns('Reply')
I18n.expects(:translate).with(
:"reply.title",
{ :count => 1,
:default => [:'topic.title', 'Title'],
:scope => [:activerecord, :attributes]
}
).returns('Title')
I18n.expects(:translate).with(
:"models.reply.attributes.title.invalid",
:value => nil,
:scope => [:activerecord, :errors],
:default => [
:"models.reply.invalid",
:"models.topic.attributes.title.invalid",
:"models.topic.invalid",
'default from class def',
:"messages.invalid"],
:model => 'Reply',
:attribute => 'Title'
).returns("default from class def")
Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def'
end
# validates_uniqueness_of w/ mocha
def test_validates_uniqueness_of_generates_message
@ -115,6 +47,25 @@ def test_validates_uniqueness_of_generates_message_with_custom_default_message
@topic.valid?
end
# validates_uniqueness_of w/o mocha
def test_validates_associated_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :errors => {:models => {:topic => {:attributes => {:title => {:taken => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:taken => 'global message'}}
Topic.validates_uniqueness_of :title
unique_topic.valid?
assert_equal ['custom message'], unique_topic.errors[:replies]
end
def test_validates_associated_finds_global_default_translation
I18n.backend.store_translations 'en', :errors => {:messages => {:taken => 'global message'}}
Topic.validates_uniqueness_of :title
unique_topic.valid?
assert_equal ['global message'], unique_topic.errors[:replies]
end
# validates_associated w/ mocha
def test_validates_associated_generates_message
@ -132,8 +83,8 @@ def test_validates_associated_generates_message_with_custom_default_message
# validates_associated w/o mocha
def test_validates_associated_finds_custom_model_key_translation
I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}}
Topic.validates_associated :replies
replied_topic.valid?
@ -141,7 +92,7 @@ def test_validates_associated_finds_custom_model_key_translation
end
def test_validates_associated_finds_global_default_translation
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}}
Topic.validates_associated :replies
replied_topic.valid?