我是Ruby的新手,我没有看到所有这些类和方法之间的联系。您能否解释一下每种方法的确切含义:
module Naming
# Returns an ActiveModel::Name object for module. It can be
# used to retrieve all kinds of naming-related information.
def model_name
@_model_name ||= begin
namespace = self.parents.detect do |n|
n.respond_to?(:use_relative_model_naming?) && n.use_relativve_model_naming?
end
ActiveModel::Name.new(self, namespace)
end
end
# Returns the plural class name of a record or class. Examples:
#
# ActiveModel::Naming.plural(post) # => "posts"
# ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
def self.plural(record_or_class)
model_name_from_record_or_class(record_or_class).plural
end
# Returns the singular class name of a record or class. Examples:
#
# ActiveModel::Naming.singular(post) # => "post"
# ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
def self.singular(record_or_class)
model_name_from_record_or_class(record_or_class).singular
end
# Identifies whether the class name of a record or class is uncountable. Examples:
#
# ActiveModel::Naming.uncountable?(Sheep) # => true
# ActiveModel::Naming.uncountable?(Post) => false
def self.uncountable?(record_or_class)
plural(record_or_class) == singular(record_or_class)
end
# 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
def self.singular_route_key(record_or_class)
model_name_from_record_or_class(record_or_class).singular_route_key
end
# 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.
def self.route_key(record_or_class)
model_name_from_record_or_class(record_or_class).route_key
end
# 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
def self.param_key(record_or_class)
model_name_from_record_or_class(record_or_class).param_key
end
private
def self.model_name_from_record_or_class(record_or_class)
(record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
end
def self.convert_to_model(object)
object.respond_to?(:to_model) ? object.to_model : object
end
end
end
我知道每种方法都有评论,但我仍然无法理解基本元。
答案 0 :(得分:1)
此模块是ActiveModel的一部分,它有助于确保命名约定。
所有这一切的目标是提供一个标准界面,帮助从单个对象中推断:
render
背后的魔力,例如,从控制器名称中推断出视图的位置)form_for
帮助程序很难再解释这个模块,因为它依赖于命名约定的独立逻辑位无处不在。