Active Model Naming

Creates a model_name method on your object.

To implement, just extend ActiveModel::Naming in your object:

  class BookCover
    extend ActiveModel::Naming
  end

  BookCover.model_name        # => "BookCover"
  BookCover.model_name.human  # => "Book cover"

  BookCover.model_name.i18n_key              # => :book_cover
  BookModule::BookCover.model_name.i18n_key  # => :"book_module/book_cover"

Providing the functionality that ActiveModel::Naming provides in your object is required to pass the Active Model Lint test. So either extending the provided method below, or rolling your own is required.

Methods
M
P
R
S
U
Class Public methods
param_key(record_or_class)

Returns string to use for params names. It differs for namespaced models regarding whether it’s inside isolated engine.

For isolated engine: ActiveModel::Naming.param_key(Blog::Post) #=> post

For shared engine: ActiveModel::Naming.param_key(Blog::Post) #=> blog_post

     # File activemodel/lib/active_model/naming.rb, line 157
157:     def self.param_key(record_or_class)
158:       model_name_from_record_or_class(record_or_class).param_key
159:     end
plural(record_or_class)

Returns the plural class name of a record or class. Examples:

  ActiveModel::Naming.plural(post)             # => "posts"
  ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
     # File activemodel/lib/active_model/naming.rb, line 102
102:     def self.plural(record_or_class)
103:       model_name_from_record_or_class(record_or_class).plural
104:     end
route_key(record_or_class)

Returns string to use while generating route names. It differs for namespaced models regarding whether it’s inside isolated engine.

For isolated engine: ActiveModel::Naming.route_key(Blog::Post) #=> posts

For shared engine: ActiveModel::Naming.route_key(Blog::Post) #=> blog_posts

The route key also considers if the noun is uncountable and, in such cases, automatically appends _index.

     # File activemodel/lib/active_model/naming.rb, line 145
145:     def self.route_key(record_or_class)
146:       model_name_from_record_or_class(record_or_class).route_key
147:     end
singular(record_or_class)

Returns the singular class name of a record or class. Examples:

  ActiveModel::Naming.singular(post)             # => "post"
  ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
     # File activemodel/lib/active_model/naming.rb, line 110
110:     def self.singular(record_or_class)
111:       model_name_from_record_or_class(record_or_class).singular
112:     end
singular_route_key(record_or_class)

Returns string to use while generating route names. It differs for namespaced models regarding whether it’s inside isolated engine.

For isolated engine: ActiveModel::Naming.route_key(Blog::Post) #=> post

For shared engine: ActiveModel::Naming.route_key(Blog::Post) #=> blog_post

     # File activemodel/lib/active_model/naming.rb, line 130
130:     def self.singular_route_key(record_or_class)
131:       model_name_from_record_or_class(record_or_class).singular_route_key
132:     end
uncountable?(record_or_class)

Identifies whether the class name of a record or class is uncountable. Examples:

  ActiveModel::Naming.uncountable?(Sheep) # => true
  ActiveModel::Naming.uncountable?(Post) => false
     # File activemodel/lib/active_model/naming.rb, line 118
118:     def self.uncountable?(record_or_class)
119:       plural(record_or_class) == singular(record_or_class)
120:     end
Instance Public methods
model_name()

Returns an ActiveModel::Name object for module. It can be used to retrieve all kinds of naming-related information.

    # File activemodel/lib/active_model/naming.rb, line 89
89:     def model_name
90:       @_model_name ||= begin
91:         namespace = self.parents.detect do |n|
92:           n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
93:         end
94:         ActiveModel::Name.new(self, namespace)
95:       end
96:     end