1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added Base.human_name method

This commit is contained in:
Iain Hecker 2008-08-16 20:01:42 +02:00 committed by Sven Fuchs
parent c379582064
commit ae8a35d8f6
2 changed files with 59 additions and 0 deletions

View file

@ -1249,6 +1249,19 @@ module ActiveRecord #:nodoc:
options[:count] ||= 1
I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes]))
end
# Transform the modelname into a more humane format, using I18n.
# Defaults to the basic humanize method.
# Default scope of the translation is activerecord.models
# Specify +options+ with additional translating options.
def human_name(options = {})
defaults = self_and_descendents_from_active_record.map do |klass|
:"#{klass.name.underscore}"
end
defaults << self.name.humanize
I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options))
end
# True if this isn't a concrete subclass needing a STI type condition.
def descends_from_active_record?
if superclass.abstract_class?

View file

@ -0,0 +1,46 @@
require "cases/helper"
require 'models/topic'
require 'models/reply'
class ActiveRecordI18nTests < Test::Unit::TestCase
def setup
reset_translations
end
def test_translated_model_attributes
I18n.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } }
assert_equal 'topic title attribute', Topic.human_attribute_name('title')
end
def test_translated_model_attributes_with_sti
I18n.store_translations 'en-US', :activerecord => {:attributes => {:reply => {:title => 'reply title attribute'} } }
assert_equal 'reply title attribute', Reply.human_attribute_name('title')
end
def test_translated_model_attributes_with_sti_fallback
I18n.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } }
assert_equal 'topic title attribute', Reply.human_attribute_name('title')
end
def test_translated_model_names
I18n.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} }
assert_equal 'topic model', Topic.human_name
end
def test_translated_model_names_with_sti
I18n.store_translations 'en-US', :activerecord => {:models => {:reply => 'reply model'} }
assert_equal 'reply model', Reply.human_name
end
def test_translated_model_names_with_sti_fallback
I18n.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} }
assert_equal 'topic model', Reply.human_name
end
private
def reset_translations
I18n.backend.send(:class_variable_set, :@@translations, {})
end
end