跳至内容 跳至搜索

Active Model 命名

在你的对象上创建 model_name 方法。

要实现,只需在你的对象中扩展 ActiveModel::Naming

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"

在你的对象中提供 ActiveModel::Naming 提供的功能是通过 Active Model Lint 测试的必要条件。因此,要么扩展下面提供的代码,要么自己编写代码。

方法
M
P
R
S
U

类公共方法

param_key(record_or_class)

返回用于参数名称的字符串。对于命名空间模型,它根据是否在隔离的引擎中而有所不同。

# 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 338
def self.param_key(record_or_class)
  model_name_from_record_or_class(record_or_class).param_key
end

plural(record_or_class)

返回记录或类的复数类名。

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

route_key(record_or_class)

返回在生成路由名称时使用的字符串。对于命名空间模型,它根据是否在隔离的引擎中而有所不同。

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

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

路由键还考虑名词是否不可数,在这种情况下,会自动附加 _index。

# File activemodel/lib/active_model/naming.rb, line 326
def self.route_key(record_or_class)
  model_name_from_record_or_class(record_or_class).route_key
end

singular(record_or_class)

返回记录或类的单数类名。

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

singular_route_key(record_or_class)

返回在生成路由名称时使用的字符串。对于命名空间模型,它根据是否在隔离的引擎中而有所不同。

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

# For shared engine:
ActiveModel::Naming.singular_route_key(Blog::Post) # => "blog_post"
# File activemodel/lib/active_model/naming.rb, line 311
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)

识别记录或类的类名是否不可数。

ActiveModel::Naming.uncountable?(Sheep) # => true
ActiveModel::Naming.uncountable?(Post)  # => false
# File activemodel/lib/active_model/naming.rb, line 299
def self.uncountable?(record_or_class)
  model_name_from_record_or_class(record_or_class).uncountable?
end

实例公共方法

model_name()

返回模块的 ActiveModel::Name 对象。它可以用于检索所有与命名相关的各种信息(有关更多信息,请参阅 ActiveModel::Name)。

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 270
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