class ActiveModel::Name

Attributes

cache_key[R]
collection[R]
element[R]
i18n_key[R]
param_key[R]
partial_path[R]
plural[R]
route_key[R]
singular[R]
singular_route_key[R]

Public Class Methods

new(klass, namespace = nil, name = nil) click to toggle source
Calls superclass method
# File lib/active_model/naming.rb, line 15
def initialize(klass, namespace = nil, name = nil)
  name ||= klass.name

  raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if name.blank?

  super(name)

  @unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace
  @klass        = klass
  @singular     = _singularize(self).freeze
  @plural       = ActiveSupport::Inflector.pluralize(@singular).freeze
  @element      = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
  @human        = ActiveSupport::Inflector.humanize(@element).freeze
  @collection   = ActiveSupport::Inflector.tableize(self).freeze
  @partial_path = "#{@collection}/#{@element}".freeze
  @param_key    = (namespace ? _singularize(@unnamespaced) : @singular).freeze
  @i18n_key     = self.underscore.to_sym

  @route_key          = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural.dup)
  @singular_route_key = ActiveSupport::Inflector.singularize(@route_key).freeze
  @route_key << "_index" if @plural == @singular
  @route_key.freeze
end

Public Instance Methods

human(options={}) click to toggle source

Transform the model name into a more humane format, using I18n. By default, it will underscore then humanize the class name

BlogPost.model_name.human # => "Blog post"

Specify options with additional translating options.

# File lib/active_model/naming.rb, line 45
def human(options={})
  return @human unless @klass.respond_to?(:lookup_ancestors) &&
                       @klass.respond_to?(:i18n_scope)

  defaults = @klass.lookup_ancestors.map do |klass|
    klass.model_name.i18n_key
  end

  defaults << options[:default] if options[:default]
  defaults << @human

  options = {:scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults}.merge(options.except(:default))
  I18n.translate(defaults.shift, options)
end

Private Instance Methods

_singularize(string, replacement='_') click to toggle source
# File lib/active_model/naming.rb, line 62
def _singularize(string, replacement='_')
  ActiveSupport::Inflector.underscore(string).tr('/', replacement)
end