Module: ActiveModel::Naming
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Extended In:
| |
|
Included In:
| |
| Defined in: | activemodel/lib/active_model/naming.rb |
Overview
=====================
Creates a #model_name method on your object.
To implement, just extend Naming in your object:
class BookCover
extend ActiveModel::Naming
end
{BookCover.model_name}.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 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.
Class Method Summary
-
.param_key(record_or_class)
Returns string to use for params names.
-
.plural(record_or_class)
Returns the plural class name of a record or class.
-
.route_key(record_or_class)
Returns string to use while generating route names.
-
.singular(record_or_class)
Returns the singular class name of a record or class.
-
.singular_route_key(record_or_class)
Returns string to use while generating route names.
-
.uncountable?(record_or_class) ⇒ Boolean
Identifies whether the class name of a record or class is uncountable.
- .extended(base) Internal use only
- .model_name_from_record_or_class(record_or_class) private Internal use only
Instance Method Summary
-
#model_name
Returns an
Nameobject for module. - #inherited(base) private
Class Method Details
.extended(base)
# File 'activemodel/lib/active_model/naming.rb', line 287
def self.extended(base) # :nodoc: base.silence_redefinition_of_method :model_name base.delegate :model_name, to: :class end
.model_name_from_record_or_class(record_or_class) (private)
# File 'activemodel/lib/active_model/naming.rb', line 391
def self.model_name_from_record_or_class(record_or_class) # :nodoc: if record_or_class.respond_to?(:to_model) record_or_class.to_model.model_name else record_or_class.model_name end end
.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:
<code>param_key</code>(Blog::Post) # => "post"
#### For shared engine:
<code>param_key</code>(Blog::Post) # => "blog_post"
# File 'activemodel/lib/active_model/naming.rb', line 387
def self.param_key(record_or_class) model_name_from_record_or_class(record_or_class).param_key end
.plural(record_or_class)
Returns the plural class name of a record or class.
<code>plural</code>(post) # => "posts"
<code>plural</code>(Highrise::Person) # => "highrise_people"
# File 'activemodel/lib/active_model/naming.rb', line 322
def self.plural(record_or_class) model_name_from_record_or_class(record_or_class).plural 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:
<code>route_key</code>(Blog::Post) # => "posts"
#### For shared engine:
<code>route_key</code>(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 373
def self.route_key(record_or_class) model_name_from_record_or_class(record_or_class).route_key end
.singular(record_or_class)
Returns the singular class name of a record or class.
<code>singular</code>(post) # => "post"
<code>singular</code>(Highrise::Person) # => "highrise_person"
# File 'activemodel/lib/active_model/naming.rb', line 332
def self.singular(record_or_class) model_name_from_record_or_class(record_or_class).singular 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:
<code>singular_route_key</code>(Blog::Post) # => "post"
#### For shared engine:
<code>singular_route_key</code>(Blog::Post) # => "blog_post"
# File 'activemodel/lib/active_model/naming.rb', line 356
def self.singular_route_key(record_or_class) model_name_from_record_or_class(record_or_class).singular_route_key end
.uncountable?(record_or_class) ⇒ Boolean
Identifies whether the class name of a record or class is uncountable.
<code>uncountable?</code>(Sheep) # => true
<code>uncountable?</code>(Post) # => false
# File 'activemodel/lib/active_model/naming.rb', line 342
def self.uncountable?(record_or_class) model_name_from_record_or_class(record_or_class).uncountable? end
Instance Method Details
#inherited(base) (private)
[ GitHub ]# File 'activemodel/lib/active_model/naming.rb', line 401
def inherited(base) super base.class_eval do @_model_name = nil end end
#model_name
Returns an Name object for module. It can be
used to retrieve all kinds of naming-related information
(See Name for more information).
class Person
extend ActiveModel::Naming
end
{Person.model_name}.name # => "Person"
{Person.model_name}.class # => {ActiveModel::Name}
{Person.model_name}.singular # => "person"
{Person.model_name}.plural # => "people"
# File 'activemodel/lib/active_model/naming.rb', line 306
def model_name return @_model_name if @_model_name namespace = module_parents.detect do |n| n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming? end @_model_name = ActiveModel::Name.new(self, namespace) ActiveSupport::Ractors.make_shareable(@_model_name) end