Permalink
Browse files

translates when a message symbol has been set on builtin validations

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent a1eb4e1 commit 76797b443929005f43512a147e97f02f3145ed81 Iain Hecker committed with jeremy Aug 31, 2008
Showing with 35 additions and 1 deletion.
  1. +2 −1 activerecord/lib/active_record/validations.rb
  2. +33 −0 activerecord/test/cases/validations_i18n_test.rb
@@ -87,6 +87,8 @@ def add_on_blank(attributes, custom_message = nil)
# </ol>
def generate_message(attribute, message = :invalid, options = {})
+ message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
+
defaults = @base.class.self_and_descendents_from_active_record.map do |klass|
[ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}",
:"models.#{klass.name.underscore}.#{message}" ]
@@ -95,7 +97,6 @@ def generate_message(attribute, message = :invalid, options = {})
defaults << options.delete(:default)
defaults = defaults.compact.flatten << :"messages.#{message}"
- model_name = @base.class.name
key = defaults.shift
value = @base.respond_to?(attribute) ? @base.send(attribute) : nil
@@ -675,6 +675,38 @@ def test_validates_associated_finds_global_default_translation
replied_topic.valid?
assert_equal 'global message', replied_topic.errors.on(:replies)
end
+
+ def test_validations_with_message_symbol_must_translate
+ I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom_error => "I am a custom error"}}}
+ Topic.validates_presence_of :title, :message => :custom_error
+ @topic.title = nil
+ @topic.valid?
+ assert_equal "I am a custom error", @topic.errors.on(:title)
+ end
+
+ def test_validates_with_message_symbol_must_translate_per_attribute
+ I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}}
+ Topic.validates_presence_of :title, :message => :custom_error
+ @topic.title = nil
+ @topic.valid?
+ assert_equal "I am a custom error", @topic.errors.on(:title)
+ end
+
+ def test_validates_with_message_symbol_must_translate_per_model
+ I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:custom_error => "I am a custom error"}}}}
+ Topic.validates_presence_of :title, :message => :custom_error
+ @topic.title = nil
+ @topic.valid?
+ assert_equal "I am a custom error", @topic.errors.on(:title)
+ end
+
+ def test_validates_with_message_string
+ Topic.validates_presence_of :title, :message => "I am a custom error"
+ @topic.title = nil
+ @topic.valid?
+ assert_equal "I am a custom error", @topic.errors.on(:title)
+ end
+
end
class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase
@@ -855,4 +887,5 @@ def test_generate_message_odd_with_default_message
def test_generate_message_even_with_default_message
assert_equal "must be even", @topic.errors.generate_message(:title, :even, :default => nil, :value => 'title', :count => 10)
end
+
end

0 comments on commit 76797b4

Please sign in to comment.