跳至内容 跳至搜索

Action Text FixtureSet

Fixtures 是组织要针对其进行测试的数据的一种方式;简而言之,即示例数据。

要详细了解 fixtures,请阅读 ActiveRecord::FixtureSet 文档。

YAML

与其他 Active Record 支持的模型类似,ActionText::RichText 记录继承自 ActiveRecord::Base 实例,因此可以通过 fixtures 填充。

考虑一个 Article

class Article < ApplicationRecord
  has_rich_text :content
end

要为相关的 content 声明 fixture 数据,请首先在 test/fixtures/articles.yml 中为 Article 实例声明 fixture 数据

first:
  title: An Article

然后在 test/fixtures/action_text/rich_texts.yml 中声明 ActionText::RichText fixture 数据,确保将每个条目的 record: 键声明为多态关系

first:
  record: first (Article)
  name: content
  body: <div>Hello, world.</div>

处理后,Active Record 将为每个 fixture 条目插入数据库记录,并将确保 Action Text 关系完好无损。

方法
A

类公共方法

attachment(fixture_set_name, label, column_type: :integer)

Fixtures 支持 Action Text 附件作为其 body HTML 的一部分。

示例

例如,考虑在 test/fixtures/articles.yml 中声明的第二个 Article fixture

second:
  title: Another Article

你可以通过在 test/fixtures/action_text/rich_texts.ymlbody: 值中嵌入对 ActionText::FixtureSet.attachment 的调用,将 articles(:first) 的提及附加到 secondcontent

second:
  record: second (Article)
  name: content
  body: <div>Hello, <%= ActionText::FixtureSet.attachment("articles", :first) %></div>
# File actiontext/lib/action_text/fixture_set.rb, line 61
def self.attachment(fixture_set_name, label, column_type: :integer)
  signed_global_id = ActiveRecord::FixtureSet.signed_global_id fixture_set_name, label,
    column_type: column_type, for: ActionText::Attachable::LOCATOR_NAME

  %(<action-text-attachment sgid="#{signed_global_id}"></action-text-attachment>)
end