Permalink
Browse files

allow ActiveRecord#RecordInvalid exception message to be localized

[#2754 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent d725ad3 commit a4838ee466b18ef04d0a244bec292c1d45c4bc0f Sven Fuchs committed with jeremy Aug 26, 2009
@@ -23,6 +23,7 @@ en:
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.
# You can define own errors for models or model attributes.
@@ -10,7 +10,8 @@ class RecordInvalid < ActiveRecordError
attr_reader :record
def initialize(record)
@record = record
- super("Validation failed: #{@record.errors.full_messages.join(", ")}")
+ errors = @record.errors.full_messages.join(I18n.t('support.array.words_connector', :default => ', '))
+ super(I18n.t('activerecord.errors.messages.record_invalid', :errors => errors))
end
end
@@ -736,10 +736,9 @@ def test_validates_with_message_string
@topic.valid?
assert_equal "I am a custom error", @topic.errors.on(:title)
end
-
end
-class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase
+class ActiveRecordValidationsGenerateMessageI18nTests < ActiveSupport::TestCase
def setup
reset_callbacks Topic
@topic = Topic.new
@@ -917,5 +916,12 @@ 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
+ # ActiveRecord#RecordInvalid exception
+ test "RecordInvalid exception can be localized" do
+ topic = Topic.new
+ topic.errors.add(:title, :invalid)
+ topic.errors.add(:title, :blank)
+ assert_equal "Validation failed: Title is invalid, Title can't be blank", ActiveRecord::RecordInvalid.new(topic).message
+ end
end

0 comments on commit a4838ee

Please sign in to comment.