2020-10-30 15:53:35 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActionText
|
2021-01-24 05:16:27 -05:00
|
|
|
# Fixtures are a way of organizing data that you want to test against; in
|
|
|
|
# short, sample data.
|
|
|
|
#
|
|
|
|
# To learn more about fixtures, read the
|
|
|
|
# {ActiveRecord::FixtureSet}[rdoc-ref:ActiveRecord::FixtureSet] documentation.
|
|
|
|
#
|
|
|
|
# === YAML
|
|
|
|
#
|
|
|
|
# Like other Active Record-backed models, ActionText::RichText records inherit
|
2021-10-10 13:32:52 -04:00
|
|
|
# from ActiveRecord::Base instances and can therefore be populated by
|
2021-01-24 05:16:27 -05:00
|
|
|
# fixtures.
|
|
|
|
#
|
2021-10-10 13:32:52 -04:00
|
|
|
# Consider an <tt>Article</tt> class:
|
2021-01-24 05:16:27 -05:00
|
|
|
#
|
|
|
|
# class Article < ApplicationRecord
|
|
|
|
# has_rich_text :content
|
|
|
|
# end
|
|
|
|
#
|
2021-10-10 13:32:52 -04:00
|
|
|
# To declare fixture data for the related <tt>content</tt>, first declare fixture
|
|
|
|
# data for <tt>Article</tt> instances in <tt>test/fixtures/articles.yml</tt>:
|
|
|
|
#
|
2021-01-24 05:16:27 -05:00
|
|
|
# first:
|
|
|
|
# title: An Article
|
|
|
|
#
|
2021-10-10 13:32:52 -04:00
|
|
|
# Then declare the <tt>ActionText::RichText</tt> fixture data in
|
|
|
|
# <tt>test/fixtures/action_text/rich_texts.yml</tt>, making sure to declare
|
|
|
|
# each entry's <tt>record:</tt> key as a polymorphic relationship:
|
|
|
|
#
|
|
|
|
# first:
|
2021-01-24 05:16:27 -05:00
|
|
|
# record: first (Article)
|
|
|
|
# name: content
|
|
|
|
# body: <div>Hello, world.</div>
|
|
|
|
#
|
|
|
|
# When processed, Active Record will insert database records for each fixture
|
|
|
|
# entry and will ensure the Action Text relationship is intact.
|
2020-10-30 15:53:35 -04:00
|
|
|
class FixtureSet
|
2021-01-24 05:16:27 -05:00
|
|
|
# Fixtures support Action Text attachments as part of their <tt>body</tt>
|
|
|
|
# HTML.
|
|
|
|
#
|
|
|
|
# === Examples
|
|
|
|
#
|
2021-10-10 13:32:52 -04:00
|
|
|
# For example, consider a second <tt>Article</tt> fixture declared in
|
|
|
|
# <tt>test/fixtures/articles.yml</tt>:
|
2021-01-24 05:16:27 -05:00
|
|
|
#
|
|
|
|
# second:
|
|
|
|
# title: Another Article
|
|
|
|
#
|
2021-10-10 13:32:52 -04:00
|
|
|
# You can attach a mention of <tt>articles(:first)</tt> to <tt>second</tt>'s
|
|
|
|
# <tt>content</tt> by embedding a call to <tt>ActionText::FixtureSet.attachment</tt>
|
|
|
|
# in the <tt>body:</tt> value in <tt>test/fixtures/action_text/rich_texts.yml</tt>:
|
|
|
|
#
|
|
|
|
# second:
|
2021-01-24 05:16:27 -05:00
|
|
|
# record: second (Article)
|
|
|
|
# name: content
|
2021-04-11 10:56:31 -04:00
|
|
|
# body: <div>Hello, <%= ActionText::FixtureSet.attachment("articles", :first) %></div>
|
2021-10-10 13:32:52 -04:00
|
|
|
#
|
2020-10-30 15:53:35 -04:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|