跳至內容 跳至搜尋

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 測試的必要條件。因此,必須延伸以下提供的 method,或自己建立。

方法
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