class ActiveModel::Name - activemodel-4.2.4 Documentation

# class ActiveModel::Name

### Attributes

cache_key[R]
collection[R]
element[R]
i18n_key[R]
name[R]
param_key[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

Returns a new ActiveModel::Name instance. By default, the `namespace` and `name` option will take the namespace and name of the given class respectively.

    module Foo
      class Bar
      end
    end

    ActiveModel::Name.new(Foo::Bar).to_s
    # => "Foo::Bar"

    # File lib/active_model/naming.rb, line 146
    def initialize(klass, namespace = nil, name = nil)
      @name = name || klass.name

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

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

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

### Public Instance Methods

!~(regexp) click to toggle source

Equivalent to `String#!~`. Match the class name against the given regexp. Returns `true` if there is no match, otherwise `false`.

    class BlogPost
      extend ActiveModel::Naming
    end

    BlogPost.model_name !~ /Post/ # => false
    BlogPost.model_name !~ /\d/   # => true

    # File lib/active_model/naming.rb, line 95

==(other) click to toggle source

Equivalent to `String#<=>`.

    class BlogPost
      extend ActiveModel::Naming
    end

    BlogPost.model_name <=> 'BlogPost'  # => 0
    BlogPost.model_name <=> 'Blog'      # => 1
    BlogPost.model_name <=> 'BlogPosts' # => -1

    # File lib/active_model/naming.rb, line 62

==(other) click to toggle source

Equivalent to `String#==`. Returns `true` if the class name and `other` are equal, otherwise `false`.

    class BlogPost
      extend ActiveModel::Naming
    end

    BlogPost.model_name == 'BlogPost'  # => true
    BlogPost.model_name == 'Blog Post' # => false

    # File lib/active_model/naming.rb, line 31

===(other) click to toggle source

Equivalent to `#==`.

    class BlogPost
      extend ActiveModel::Naming
    end

    BlogPost.model_name === 'BlogPost'  # => true
    BlogPost.model_name === 'Blog Post' # => false

    # File lib/active_model/naming.rb, line 46

=~(regexp) click to toggle source

Equivalent to `String#=~`. Match the class name against the given regexp. Returns the position where the match starts or `nil` if there is no match.

    class BlogPost
      extend ActiveModel::Naming
    end

    BlogPost.model_name =~ /Post/ # => 4
    BlogPost.model_name =~ /\d/   # => nil

    # File lib/active_model/naming.rb, line 79

eql?(other) click to toggle source

Equivalent to `String#eql?`. Returns `true` if the class name and `other` have the same length and content, otherwise `false`.

    class BlogPost
      extend ActiveModel::Naming
    end

    BlogPost.model_name.eql?('BlogPost')  # => true
    BlogPost.model_name.eql?('Blog Post') # => false

    # File lib/active_model/naming.rb, line 111

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.

    class BlogPost
      extend ActiveModel::Naming
    end

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

Specify `options` with additional translating options.

    # File lib/active_model/naming.rb, line 176
    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

to_s() click to toggle source

Returns the class name.

    class BlogPost
      extend ActiveModel::Naming
    end

    BlogPost.model_name.to_s # => "BlogPost"

    # File lib/active_model/naming.rb, line 125

to_str() click to toggle source

Equivalent to `to_s`.

    # File lib/active_model/naming.rb, line 132
    delegate :==, :===, :<=>, :=~, :"!~", :eql?, :to_s,
             :to_str, :as_json, to: :name

### Private Instance Methods

_singularize(string, replacement='_') click to toggle source

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