class ActiveStorage::FixtureSet
entry and will ensure the Active Storage relationship is intact.
When processed, Active Record will insert database records for each fixture
blob: first_thumbnail_blob
record: first (Article)
name: thumbnail
first_thumbnail_attachment:
# fixtures/active_storage/attachments.yml
first_thumbnail_blob: <%= ActiveStorage::FixtureSet.blob filename: “first.png” %>
# fixtures/active_storage/blobs.yml
end
has_one_attached :thumbnail
class Article < ApplicationRecord
# app/models/article.rb
and ActiveStorage::Blob records:
fixture data, as well as fixture data for related ActiveStorage::Attachment
Consider a hypothetical Article
model class, its related
can be populated by fixtures.
instances and therefore
records inherit from
and
Like other Active Record-backed models,
=== YAML
documentation.
To learn more about fixtures, read the
short, sample data.
Fixtures are a way of organizing data that you want to test against; in
def self.blob(filename:, **attributes)
) %>
service_name: "public"
content_type: "image/svg+xml",
filename: "third.svg",
third_thumbnail_blob: <%= ActiveStorage::FixtureSet.blob(
) %>
filename: "second.svg",
second_thumbnail_blob: <%= ActiveStorage::FixtureSet.blob(
# tests/fixtures/action_text/blobs.yml
=== Examples
the file to the Service
by ActiveSupport::Testing::FileFixtures.file_fixture, and upload
instance's attributes, resolve the file relative to the directory mentioned
Generate a YAML-encoded representation of an ActiveStorage::Blob
def self.blob(filename:, **attributes) new.prepare Blob.new(filename: filename, key: generate_unique_secure_token), **attributes end
def prepare(instance, **attributes)
def prepare(instance, **attributes) io = file_fixture(instance.filename.to_s).open instance.unfurl(io) instance.assign_attributes(attributes) instance.upload_without_unfurling(io) instance.attributes.transform_values { |value| value.is_a?(Hash) ? value.to_json : value }.compact.to_json end