Skip to content

Commit

Permalink
incorporate #translate usage with several default keys
Browse files Browse the repository at this point in the history
(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
Sven Fuchs committed Jun 21, 2008
1 parent 428aa24 commit 8bfdabb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 55 deletions.
19 changes: 16 additions & 3 deletions activerecord/lib/active_record/validations.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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


Expand Down
86 changes: 35 additions & 51 deletions activerecord/test/cases/validations_i18n_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand All @@ -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?
Expand All @@ -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'})
Expand All @@ -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})
Expand All @@ -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})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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?
Expand All @@ -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?
Expand All @@ -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?
Expand All @@ -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?
Expand All @@ -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?
Expand All @@ -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?
Expand All @@ -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?
Expand All @@ -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?
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/vendor/i18n-0.0.1
Submodule i18n-0.0.1 updated from 8e43af to 20c331

0 comments on commit 8bfdabb

Please sign in to comment.