1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

translates when a message symbol has been set on builtin validations

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
Iain Hecker 2008-08-31 12:14:24 +02:00 committed by Jeremy Kemper
parent a1eb4e11c2
commit 76797b4439
2 changed files with 35 additions and 1 deletions

View file

@ -87,6 +87,8 @@ module ActiveRecord
# </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 @@ module ActiveRecord
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

View file

@ -675,6 +675,38 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
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 @@ class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase
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