Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update I18n defaults for activerecord.errors.messages.record_invalid #5274

Merged
merged 3 commits into from

2 participants

Christopher Dell José Valim
Christopher Dell

Allows translations of activerecord.errors.messages.record_invalid to in errors.messages.record_invalid

José Valim
Owner
Christopher Dell

OK, test added

.../validations/i18n_generate_message_validation_test.rb
@@ -8,6 +8,15 @@ def setup
I18n.backend = I18n::Backend::Simple.new
end
+ def reset_i18n_load_path(&block)
+ @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend
+ I18n.load_path.clear
+ I18n.backend = I18n::Backend::Simple.new
+ yield
José Valim Owner

The following two lines should likely be inside an ensure block. So if a test fail its state doesn't leak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
.../validations/i18n_generate_message_validation_test.rb
@@ -8,6 +8,15 @@ def setup
I18n.backend = I18n::Backend::Simple.new
end
+ def reset_i18n_load_path(&block)
José Valim Owner

No need to receive the block as argument here, since we are using yield. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Christopher Dell

Good call, test tweaked.

José Valim josevalim merged commit c5a47b3 into from
José Valim
Owner

Merged, tks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 4, 2012
  1. Christopher Dell

    Allow translations of activerecord.errors.messages.record_invalid to …

    tigrish authored
    …be looked up in errors.messages.record_invalid
Commits on Mar 5, 2012
  1. Christopher Dell
  2. Christopher Dell
This page is out of date. Refresh to see the latest.
2  activerecord/lib/active_record/validations.rb
View
@@ -14,7 +14,7 @@ class RecordInvalid < ActiveRecordError
def initialize(record)
@record = record
errors = @record.errors.full_messages.join(", ")
- super(I18n.t("activerecord.errors.messages.record_invalid", :errors => errors))
+ super(I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", :errors => errors, :default => :"errors.messages.record_invalid"))
end
end
19 activerecord/test/cases/validations/i18n_generate_message_validation_test.rb
View
@@ -8,6 +8,16 @@ def setup
I18n.backend = I18n::Backend::Simple.new
end
+ def reset_i18n_load_path
+ @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend
+ I18n.load_path.clear
+ I18n.backend = I18n::Backend::Simple.new
+ yield
+ ensure
+ I18n.load_path.replace @old_load_path
+ I18n.backend = @old_backend
+ end
+
# validates_associated: generate_message(attr_name, :invalid, :message => custom_message, :value => value)
def test_generate_message_invalid_with_default_message
assert_equal 'is invalid', @topic.errors.generate_message(:title, :invalid, :value => 'title')
@@ -35,4 +45,13 @@ def test_generate_message_taken_with_custom_message
assert_equal "Validation failed: Title is invalid, Title can't be blank", ActiveRecord::RecordInvalid.new(topic).message
end
+ test "RecordInvalid exception translation falls back to the :errors namespace" do
+ reset_i18n_load_path do
+ I18n.backend.store_translations 'en', :errors => {:messages => {:record_invalid => 'fallback message'}}
+ topic = Topic.new
+ topic.errors.add(:title, :blank)
+ assert_equal "fallback message", ActiveRecord::RecordInvalid.new(topic).message
+ end
+ end
+
end
Something went wrong with that request. Please try again.