Skip to content

Commit

Permalink
allow ActiveRecord#RecordInvalid exception message to be localized
Browse files Browse the repository at this point in the history
[#2754 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
Sven Fuchs authored and jeremy committed Aug 26, 2009
1 parent d725ad3 commit a4838ee
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions activerecord/lib/active_record/locale/en.yml
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/validations.rb
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions activerecord/test/cases/validations_i18n_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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.