【发布时间】:2020-09-28 19:31:19
【问题描述】:
我想修补 Rails 6.0 以包含此 PR 的一部分:https://github.com/rails/rails/commit/4dba136c83cc808282625c0d5b195ce5e0bbaa68
我只使用直接上传,所以我现在只修补create_before_direct_upload!。这是我尝试过的:
- 在
initializers/active_storage.rb
module BlobOverride
class << self
def create_before_direct_upload!(key: nil, filename:, byte_size:, checksum:, content_type: nil, metadata: nil)
puts "In Blob Override Patch"
byebug
create! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, metadata: metadata
end
end
end
ActiveStorage::Blob.prepend(BlobOverride)
这会返回 undefined methodhas_one_attached'` 错误,我在这里找到了一个 github 问题:https://github.com/rails/rails/issues/38876 这基本上是说您不能使用从初始化程序加载模型。
2。然后我尝试以这种方式加载模块:
ActiveSupport.on_load(:active_storage_blob) do
ActiveStorage::Blob.prepend(BlobOverride)
end
我没有收到错误,但我的补丁没有被击中。
3. 我试过这个:
Rails.application.config.to_prepare do
require 'active_storage/blob'
ActiveStorage::Blob.class_eval do
def create_before_direct_upload!(key: nil, filename:, byte_size:, checksum:, content_type: nil, metadata: nil)
puts "in create before direct upload patch"
byebug
create! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, metadata: metadata
end
end
end
没有错误,未命中修补方法。
TLDR
如何修补活动存储上的 blob 模型以支持自定义密钥?由于某种原因,标准的猴子补丁无法正常工作。
【问题讨论】:
-
运气好吗?我正在尝试做同样的事情。
标签: ruby-on-rails rails-activestorage