Action Text FixtureSet¶ ↑
夹具是一种组织数据的方法,您想用这些数据进行测试;简而言之,就是示例数据。
要了解更多关于 fixture 的信息,请阅读 ActiveRecord::FixtureSet 文档。
YAML¶ ↑
与其他基于 Active Record 的模型一样,ActionText::RichText 记录继承自 ActiveRecord::Base 实例,因此可以由 fixture 填充。
考虑一个 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 关系完整。
方法
类公共方法
attachment(fixture_set_name, label, column_type: :integer) 链接
Fixture 支持 Action Text 附件作为其 body HTML 的一部分。
示例¶ ↑
例如,考虑在 test/fixtures/articles.yml 中声明的第二个 Article fixture
second: title: Another Article
您可以通过在 test/fixtures/action_text/rich_texts.yml 的 body: 值中嵌入对 ActionText::FixtureSet.attachment 的调用,将 articles(:first) 的提及附加到 second 的 content 中
second:
record: second (Article)
name: content
body: <div>Hello, <%= ActionText::FixtureSet.attachment("articles", :first) %></div>
来源: 显示 | 在 GitHub 上
# 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 %(<#{Attachment.tag_name} sgid="#{signed_global_id}"></#{Attachment.tag_name}>) end