Created
February 23, 2012 20:29
-
-
Save jamescook/1894896 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb | |
index e548aa9..61164d9 100644 | |
--- a/activemodel/lib/active_model/errors.rb | |
+++ b/activemodel/lib/active_model/errors.rb | |
@@ -277,8 +277,16 @@ module ActiveModel | |
return message if attribute == :base | |
attr_name = attribute.to_s.gsub('.', '_').humanize | |
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) | |
+ defaults = [] | |
+ if @base.class.respond_to?(:i18n_scope) | |
+ @base.class.lookup_ancestors.map do |klass| | |
+ defaults << :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.format" | |
+ defaults << :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.format" | |
+ end | |
+ end | |
+ defaults << "%{attribute} %{message}" | |
I18n.t(:"errors.format", { | |
- :default => "%{attribute} %{message}", | |
+ :default => defaults, | |
:attribute => attr_name, | |
:message => message | |
}) | |
diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb | |
index e9f0e43..a43b30d 100644 | |
--- a/activemodel/test/cases/validations/i18n_validation_test.rb | |
+++ b/activemodel/test/cases/validations/i18n_validation_test.rb | |
@@ -55,12 +55,25 @@ class I18nValidationTest < ActiveModel::TestCase | |
assert_equal ["Person's name not found"], @person.errors.full_messages | |
end | |
- def test_errors_full_messages_uses_format | |
+ def test_errors_full_messages_uses_default_format | |
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 | |
+ def test_errors_full_messages_uses_custom_field_level_format | |
+ I18n.backend.store_translations('en', :activemodel => {:errors => {:models => {:person => {:format => "ooga booga %{attribute} %{message}"}}}}) | |
+ I18n.backend.store_translations('en', :activemodel => {:errors => {:models => {:person => {:attributes => {:name => {:format => "very custom %{attribute} %{message}"}}}}}}) | |
+ @person.errors.add('name', 'empty') | |
+ assert_equal ["very custom Name empty"], @person.errors.full_messages | |
+ end | |
+ | |
+ def test_errors_full_messages_uses_custom_model_level_format | |
+ I18n.backend.store_translations('en', :activemodel => {:errors => {:models => {:person => {:format => "ooga booga %{attribute} %{message}"}}}}) | |
+ @person.errors.add('name', 'empty') | |
+ assert_equal ["ooga booga Name empty"], @person.errors.full_messages | |
+ end | |
+ | |
# ActiveModel::Validations | |
# A set of common cases for ActiveModel::Validations message generation that |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment