Module: ActiveModel::Naming
- Included in:
- Translation
- Defined in:
- lib/active_model/naming.rb
Overview
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.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.
Class Method Summary collapse
-
.extended(base) ⇒ Object
:nodoc:.
-
.param_key(record_or_class) ⇒ Object
Returns string to use for params names.
-
.plural(record_or_class) ⇒ Object
Returns the plural class name of a record or class.
-
.route_key(record_or_class) ⇒ Object
Returns string to use while generating route names.
-
.singular(record_or_class) ⇒ Object
Returns the singular class name of a record or class.
-
.singular_route_key(record_or_class) ⇒ Object
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.
Instance Method Summary collapse
-
#model_name ⇒ Object
Returns an ActiveModel::Name object for module.
Class Method Details
.extended(base) ⇒ Object
:nodoc:
242 243 244 245 |
# File 'lib/active_model/naming.rb', line 242 def self.extended(base) # :nodoc: base.silence_redefinition_of_method :model_name base.delegate :model_name, to: :class end |
.param_key(record_or_class) ⇒ Object
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"
327 328 329 |
# File 'lib/active_model/naming.rb', line 327 def self.param_key(record_or_class) model_name_from_record_or_class(record_or_class).param_key end |
.plural(record_or_class) ⇒ Object
Returns the plural class name of a record or class.
ActiveModel::Naming.plural(post) # => "posts"
ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
272 273 274 |
# File 'lib/active_model/naming.rb', line 272 def self.plural(record_or_class) model_name_from_record_or_class(record_or_class).plural end |
.route_key(record_or_class) ⇒ Object
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.
315 316 317 |
# File 'lib/active_model/naming.rb', line 315 def self.route_key(record_or_class) model_name_from_record_or_class(record_or_class).route_key end |
.singular(record_or_class) ⇒ Object
Returns the singular class name of a record or class.
ActiveModel::Naming.singular(post) # => "post"
ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
280 281 282 |
# File 'lib/active_model/naming.rb', line 280 def self.singular(record_or_class) model_name_from_record_or_class(record_or_class).singular end |
.singular_route_key(record_or_class) ⇒ Object
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.singular_route_key(Blog::Post) # => "post"
# For shared engine:
ActiveModel::Naming.singular_route_key(Blog::Post) # => "blog_post"
300 301 302 |
# File 'lib/active_model/naming.rb', line 300 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.
ActiveModel::Naming.uncountable?(Sheep) # => true
ActiveModel::Naming.uncountable?(Post) # => false
288 289 290 |
# File 'lib/active_model/naming.rb', line 288 def self.uncountable?(record_or_class) model_name_from_record_or_class(record_or_class).uncountable? end |
Instance Method Details
#model_name ⇒ Object
Returns an ActiveModel::Name object for module. It can be used to retrieve all kinds of naming-related information (See ActiveModel::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"
259 260 261 262 263 264 265 266 |
# File 'lib/active_model/naming.rb', line 259 def model_name @_model_name ||= begin namespace = module_parents.detect do |n| n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming? end ActiveModel::Name.new(self, namespace) end end |