活动存储装置附件 [英] Activestorage fixtures attachments

查看:55
本文介绍了活动存储装置附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 测试中.我有一个只有 activestorage 的基本模型:

In rails tests. I have a basic model with only activestorage:

class User < ApplicationRecord
  has_one_attached :avatar
end

我正在努力让它成为固定装置,但没有运气(我确实有一张图片):

I'm trying to make it's fixture, but having no luck with (I do have an image there):

# users.yml
one:
  avatar: <%= File.open Rails.root.join('test', 'files', 'image.png').to_s %>

如何通过装置正确附加头像文件?

How do I properly attach an avatar file through fixtures?

推荐答案

这比任何人想象的要容易得多.我并不是要贬低任何人,因为根据这些答案我花了一些时间才弄清楚这一点.我将使用相同的数据模型来简化操作.

This is far easier than anybody is making it out to be. I don't mean to demean anybody, as it took me some time to figure this out based on these answers. I'm going to use the same data model to make it easy.

用户有一个附加的头像".假设您有这个用户固定装置:

User has one attached "avatar". Let's say you have this users fixture:

# users.yml
fred:
  name: Fred

您只需:

% mkdir test/fixtures/active_storage

现在,您只需将attachments.yml"放入和blob.yml"在那个目录中.附件"记录将引用 blob 以及用户:

Now, you just put "attachments.yml" and "blobs.yml" in that directory. The "attachment" record will reference the blob as well as the user:

# active_storage/attachments.yml
freds_picture:
  name: avatar
  record: fred (User)
  blob: freds_picture_blob

# active_storage/blobs.yml
freds_picture_blob:
  key: aabbWNGW1VrxZ8Eu4zmyw13A
  filename: fred.jpg
  content_type: image/jpeg
  metadata: '{"identified":true,"analyzed":true}'
  byte_size: 1000
  checksum: fdUivZUf74Y6pjAiemuvlg==
  service_name: local

key 在代码中是这样生成的:

The key is generated like this in code:

ActiveStorage::Blob.generate_unique_secure_token

您可以在控制台中运行它以获得上述装置的密钥.

You can run that in the console to get a key for the above fixture.

现在,这将起作用"附上图片.如果您需要实际文件存在,首先查看 config/storage.yml 以查看文件存储在哪个路径中.默认情况下,它是tmp/storage".上面的文件将存储在这里:

Now, that will "work" to have an attached picture. If you need the actual file to be there, first look in config/storage.yml to see what path the files are stored in. By default, it's "tmp/storage". The file above will be stored here:

tmp/storage/aa/bb/aabbWNGW1VrxZ8Eu4zmyw13A

要计算校验和,请参见此处:

To calculate the checksum, see here:

校验和是如何计算的在 rails ActiveStorage 的 blob 表中

md5_checksum = Digest::MD5.file('tmp/storage/aa/bb/aabbWNGW1VrxZ8Eu4zmyw13A').base64digest

可以在夹具中使用 erb 填写文件大小和校验和:

It would be possible to fill in the file size and checksum using erb in the fixture:

  byte_size: <%= File.size('tmp/storage/aa/bb/aabbWNGW1VrxZ8Eu4zmyw13A') %>
  checksum: <%= Digest::MD5.file('tmp/storage/aa/bb/aabbWNGW1VrxZ8Eu4zmyw13A').base64digest %>

请注意,您必须先将文件复制到存储目录中.测试环境的存储根目录默认为tmp/storage/,其余路径由key的前四个字符构成(即tmp/存储/aa/bb).

Note that you have to copy the file into the storage directory first. The storage root directory for the test environment is tmp/storage/ by default, with the remaining path constructed from the first four characters of the key (i.e. tmp/storage/aa/bb).

这篇关于活动存储装置附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆