module ActiveModel::Naming
def self.extended(base) # :nodoc:
def self.extended(base) # :nodoc: base.silence_redefinition_of_method :model_name base.delegate :model_name, to: :class end
def self.model_name_from_record_or_class(record_or_class) # :nodoc:
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
def self.param_key(record_or_class)
# For shared engine:
ActiveModel::Naming.param_key(Blog::Post) # => "post"
# For isolated engine:
namespaced models regarding whether it's inside isolated engine.
Returns string to use for params names. It differs for
def self.param_key(record_or_class) model_name_from_record_or_class(record_or_class).param_key end
def self.plural(record_or_class)
ActiveModel::Naming.plural(post) # => "posts"
Returns the plural class name of a record or class.
def self.plural(record_or_class) model_name_from_record_or_class(record_or_class).plural end
def self.route_key(record_or_class)
The route key also considers if the noun is uncountable and, in
ActiveModel::Naming.route_key(Blog::Post) # => "blog_posts"
# For shared engine:
ActiveModel::Naming.route_key(Blog::Post) # => "posts"
# For isolated engine:
namespaced models regarding whether it's inside isolated engine.
Returns string to use while generating route names. It differs for
def self.route_key(record_or_class) model_name_from_record_or_class(record_or_class).route_key end
def self.singular(record_or_class)
ActiveModel::Naming.singular(post) # => "post"
Returns the singular class name of a record or class.
def self.singular(record_or_class) model_name_from_record_or_class(record_or_class).singular end
def self.singular_route_key(record_or_class)
# For shared engine:
ActiveModel::Naming.singular_route_key(Blog::Post) # => "post"
# For isolated engine:
namespaced models regarding whether it's inside isolated engine.
Returns string to use while generating route names. It differs for
def self.singular_route_key(record_or_class) model_name_from_record_or_class(record_or_class).singular_route_key end
def self.uncountable?(record_or_class)
ActiveModel::Naming.uncountable?(Sheep) # => true
Identifies whether the class name of a record or class is uncountable.
def self.uncountable?(record_or_class) model_name_from_record_or_class(record_or_class).uncountable? end
def inherited(base)
def inherited(base) super base.class_eval do @_model_name = nil end end
def model_name
Person.model_name.singular # => "person"
Person.model_name.class # => ActiveModel::Name
Person.model_name.name # => "Person"
end
extend ActiveModel::Naming
class Person
(See ActiveModel::Name for more information).
used to retrieve all kinds of naming-related information
Returns an ActiveModel::Name object for module. It can be
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