Skip to content
Browse files

incorporate #translate usage with several default keys

(use first default key that resolves to a translation). this might, depending
on the backend implementation save some expensive lookups (like db lookups)
  • Loading branch information...
1 parent 428aa24 commit 8bfdabbd8b5137f91d8bcddc8c3d18961c8e316b @svenfuchs svenfuchs committed Jun 21, 2008
View
19 activerecord/lib/active_record/validations.rb
@@ -66,9 +66,11 @@ def add_on_blank(attributes, custom_message = nil)
end
def generate_message(attr, key, options = {})
- scope = [:active_record, :error_messages]
- key.t(options.merge(:scope => scope + [:custom, @base.class.name.downcase, attr])) ||
- key.t(options.merge(:scope => scope))
+ msgs = base_classes(@base.class).map{|klass| :"custom.#{klass.name.underscore}.#{attr}.#{key}"}
+ msgs << options[:default] if options[:default]
+ msgs << key
+
+ I18n.t options.merge(:default => msgs, :scope => [:active_record, :error_messages])
end
# Returns true if the specified +attribute+ has errors associated with it.
@@ -217,6 +219,17 @@ def to_xml(options={})
full_messages.each { |msg| e.error(msg) }
end
end
+
+ protected
+
+ # TODO maybe this should be on ActiveRecord::Base, maybe #self_and_descendents_from_active_record
+ def base_classes(klass)
+ classes = [klass]
+ while klass != klass.base_class
+ classes << klass = klass.superclass
+ end
+ classes
+ end
end
View
86 activerecord/test/cases/validations_i18n_test.rb
@@ -40,31 +40,15 @@ def test_errors_generate_message_translates_custom_model_attribute_key
global_scope = [:active_record, :error_messages]
custom_scope = global_scope + [:custom, 'topic', :title]
- I18n.expects(:translate).with(:invalid, :scope => custom_scope).returns 'translation'
- I18n.expects(:translate).with(:invalid, :scope => global_scope).never
-
- @topic.errors.generate_message :title, :invalid
+ I18n.expects(:t).with :scope => [:active_record, :error_messages], :default => [:"custom.topic.title.invalid", 'default from class def', :invalid]
+ @topic.errors.generate_message :title, :invalid, :default => 'default from class def'
end
- def test_errors_generate_message_given_a_custom_message_translates_custom_model_attribute_key_with_custom_message_as_default
+ def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti
custom_scope = [:active_record, :error_messages, :custom, 'topic', :title]
- I18n.expects(:translate).with(:invalid, :scope => custom_scope, :default => 'default from class def').returns 'translation'
- @topic.errors.generate_message :title, :invalid, :default => 'default from class def'
- end
-
- def test_errors_generate_message_given_no_custom_message_falls_back_to_global_default_key_translation
- global_scope = [:active_record, :error_messages]
- custom_scope = global_scope + [:custom, 'topic', :title]
-
- I18n.stubs(:translate).with(:invalid, :scope => custom_scope).returns nil
- I18n.expects(:translate).with(:invalid, :scope => global_scope)
- @topic.errors.generate_message :title, :invalid
- end
-
- def test_errors_add_given_no_message_it_translates_invalid
- I18n.expects(:translate).with(:"active_record.error_messages.invalid")
- @topic.errors.add :title
+ I18n.expects(:t).with :scope => [:active_record, :error_messages], :default => [:"custom.reply.title.invalid", :"custom.topic.title.invalid", 'default from class def', :invalid]
+ Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def'
end
def test_errors_add_on_empty_generates_message
@@ -115,7 +99,7 @@ def test_validates_confirmation_of_generates_message_with_custom_default_message
def test_validates_confirmation_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:confirmation => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:confirmation => 'global message'}}
-
+
Topic.validates_confirmation_of :title
@topic.title_confirmation = 'foo'
@topic.valid?
@@ -124,7 +108,7 @@ def test_validates_confirmation_of_finds_custom_model_key_translation
def test_validates_confirmation_of_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:confirmation => 'global message'}}
-
+
Topic.validates_confirmation_of :title
@topic.title_confirmation = 'foo'
@topic.valid?
@@ -133,13 +117,13 @@ def test_validates_confirmation_of_finds_global_default_translation
# validates_acceptance_of
-
+
def test_validates_acceptance_of_generates_message
Topic.validates_acceptance_of :title, :allow_nil => false
@topic.errors.expects(:generate_message).with(:title, :accepted, {:default => nil})
@topic.valid?
end
-
+
def test_validates_acceptance_of_generates_message_with_custom_default_message
Topic.validates_acceptance_of :title, :message => 'custom', :allow_nil => false
@topic.errors.expects(:generate_message).with(:title, :accepted, {:default => 'custom'})
@@ -149,23 +133,23 @@ def test_validates_acceptance_of_generates_message_with_custom_default_message
def test_validates_acceptance_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:accepted => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:accepted => 'global message'}}
-
+
Topic.validates_acceptance_of :title, :allow_nil => false
@topic.valid?
assert_equal 'custom message', @topic.errors.on(:title)
end
def test_validates_acceptance_of_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:accepted => 'global message'}}
-
+
Topic.validates_acceptance_of :title, :allow_nil => false
@topic.valid?
assert_equal 'global message', @topic.errors.on(:title)
end
# validates_presence_of
-
+
def test_validates_presence_of_generates_message
Topic.validates_presence_of :title
@topic.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
@@ -181,23 +165,23 @@ def test_validates_presence_of_generates_message_with_custom_default_message
def test_validates_presence_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:blank => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:blank => 'global message'}}
-
+
Topic.validates_presence_of :title
@topic.valid?
assert_equal 'custom message', @topic.errors.on(:title)
end
def test_validates_presence_of_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:blank => 'global message'}}
-
+
Topic.validates_presence_of :title
@topic.valid?
assert_equal 'global message', @topic.errors.on(:title)
end
# validates_length_of :within
-
+
def test_validates_length_of_within_generates_message
Topic.validates_length_of :title, :within => 3..5
@topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
@@ -213,15 +197,15 @@ def test_validates_length_of_within_generates_message_with_custom_default_messag
def test_validates_length_of_within_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:too_short => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:too_short => 'global message'}}
-
+
Topic.validates_length_of :title, :within => 3..5
@topic.valid?
assert_equal 'custom message', @topic.errors.on(:title)
end
def test_validates_length_of_within_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:too_short => 'global message'}}
-
+
Topic.validates_length_of :title, :within => 3..5
@topic.valid?
assert_equal 'global message', @topic.errors.on(:title)
@@ -245,15 +229,15 @@ def test_validates_length_of_is_generates_message_with_custom_default_message
def test_validates_length_of_within_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}}
-
+
Topic.validates_length_of :title, :is => 5
@topic.valid?
assert_equal 'custom message', @topic.errors.on(:title)
end
def test_validates_length_of_within_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}}
-
+
Topic.validates_length_of :title, :is => 5
@topic.valid?
assert_equal 'global message', @topic.errors.on(:title)
@@ -279,15 +263,15 @@ def test_validates_uniqueness_of_generates_message_with_custom_default_message
def test_validates_length_of_within_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}}
-
+
Topic.validates_length_of :title, :is => 5
@topic.valid?
assert_equal 'custom message', @topic.errors.on(:title)
end
def test_validates_length_of_within_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}}
-
+
Topic.validates_length_of :title, :is => 5
@topic.valid?
assert_equal 'global message', @topic.errors.on(:title)
@@ -313,15 +297,15 @@ def test_validates_format_of_generates_message_with_custom_default_message
def test_validates_format_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:invalid => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}}
-
+
Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
@topic.valid?
assert_equal 'custom message', @topic.errors.on(:title)
end
def test_validates_format_of_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}}
-
+
Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
@topic.valid?
assert_equal 'global message', @topic.errors.on(:title)
@@ -347,15 +331,15 @@ def test_validates_inclusion_of_generates_message_with_custom_default_message
def test_validates_inclusion_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:inclusion => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:inclusion => 'global message'}}
-
+
Topic.validates_inclusion_of :title, :in => %w(a b c)
@topic.valid?
assert_equal 'custom message', @topic.errors.on(:title)
end
def test_validates_inclusion_of_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:inclusion => 'global message'}}
-
+
Topic.validates_inclusion_of :title, :in => %w(a b c)
@topic.valid?
assert_equal 'global message', @topic.errors.on(:title)
@@ -381,7 +365,7 @@ def test_validates_exclusion_of_generates_message_with_custom_default_message
def test_validates_exclusion_of_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:exclusion => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:exclusion => 'global message'}}
-
+
Topic.validates_exclusion_of :title, :in => %w(a b c)
@topic.title = 'a'
@topic.valid?
@@ -390,7 +374,7 @@ def test_validates_exclusion_of_finds_custom_model_key_translation
def test_validates_exclusion_of_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:exclusion => 'global message'}}
-
+
Topic.validates_exclusion_of :title, :in => %w(a b c)
@topic.title = 'a'
@topic.valid?
@@ -417,7 +401,7 @@ def test_validates_numericality_of_only_integer_generates_message_with_custom_de
def test_validates_numericality_of_only_integer_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}}
-
+
Topic.validates_numericality_of :title, :only_integer => true
@topic.title = 'a'
@topic.valid?
@@ -426,7 +410,7 @@ def test_validates_numericality_of_only_integer_finds_custom_model_key_translati
def test_validates_numericality_of_only_integer_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}}
-
+
Topic.validates_numericality_of :title, :only_integer => true
@topic.title = 'a'
@topic.valid?
@@ -453,7 +437,7 @@ def test_validates_numericality_of_odd_generates_message_with_custom_default_mes
def test_validates_numericality_of_odd_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:odd => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:odd => 'global message'}}
-
+
Topic.validates_numericality_of :title, :only_integer => true, :odd => true
@topic.title = 0
@topic.valid?
@@ -462,7 +446,7 @@ def test_validates_numericality_of_odd_finds_custom_model_key_translation
def test_validates_numericality_of_odd_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:odd => 'global message'}}
-
+
Topic.validates_numericality_of :title, :only_integer => true, :odd => true
@topic.title = 0
@topic.valid?
@@ -489,7 +473,7 @@ def test_validates_numericality_of_odd_generates_message_with_custom_default_mes
def test_validates_numericality_of_less_than_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:less_than => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:less_than => 'global message'}}
-
+
Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
@topic.title = 1
@topic.valid?
@@ -498,7 +482,7 @@ def test_validates_numericality_of_less_than_finds_custom_model_key_translation
def test_validates_numericality_of_less_than_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:less_than => 'global message'}}
-
+
Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
@topic.title = 1
@topic.valid?
@@ -523,15 +507,15 @@ def test_validates_associated_generates_message_with_custom_default_message
def test_validates_associated_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:replies => {:invalid => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}}
-
+
Topic.validates_associated :replies
replied_topic.valid?
assert_equal 'custom message', replied_topic.errors.on(:replies)
end
def test_validates_associated_finds_global_default_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}}
-
+
Topic.validates_associated :replies
replied_topic.valid?
assert_equal 'global message', replied_topic.errors.on(:replies)
2 activesupport/lib/active_support/vendor/i18n-0.0.1
@@ -1 +1 @@
-Subproject commit 8e43afa38aa007d1de6d6acf44d43143c403d13f
+Subproject commit 20c331666b3b6a21791d4cded53c3d8654fba714

0 comments on commit 8bfdabb

Please sign in to comment.
Something went wrong with that request. Please try again.