Module: MongoMapper::Plugins::Rails::ClassMethods

Defined in:
lib/mongo_mapper/i18n.rb

Instance Method Summary collapse

Instance Method Details

#human_attribute_name(attribute_key_name, options = {}) ⇒ Object

Transforms attribute key names into a more humane format, such as “First name” instead of “first_name”. Example:

Person.human_attribute_name("first_name") # => "First name"

This used to be depricated in favor of humanize, but is now preferred, because it automatically uses the I18n module now. Specify options with additional translating options.



26
27
28
29
30
31
32
33
34
35
# File 'lib/mongo_mapper/i18n.rb', line 26

def human_attribute_name(attribute_key_name, options = {})
  defaults = self_and_descendants.map do |klass|
    :"#{klass.name.underscore}.#{attribute_key_name}"
  end
  defaults << options[:default] if options[:default]
  defaults.flatten!
  defaults << attribute_key_name.to_s.humanize
  options[:count] ||= 1
  I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:mongo_mapper, :attributes]))
end

#human_name(options = {}) ⇒ Object

Transform the modelname into a more humane format, using I18n. Defaults to the basic humanize method. Default scope of the translation is mongo_mapper.models Specify options with additional translating options.



41
42
43
44
45
46
47
# File 'lib/mongo_mapper/i18n.rb', line 41

def human_name(options = {})
  defaults = self_and_descendants.map do |klass|
    :"#{klass.name.underscore}"
  end 
  defaults << self.name.humanize
  I18n.translate(defaults.shift, {:scope => [:mongo_mapper, :models], :count => 1, :default => defaults}.merge(options))
end

#self_and_descendantsObject

nodoc:



10
11
12
13
14
15
16
17
18
19
# File 'lib/mongo_mapper/i18n.rb', line 10

def self_and_descendants#nodoc:
  klass = self
  classes = [klass]
  while klass != klass.base_class  
    classes << klass = klass.superclass
  end
  classes
rescue
  [self]
end