I need to version images for my Rails app. I'm using Carrierwave for file uploads, and PaperTrail for versioning.
我需要为我的Rails应用版本的图像。我使用Carrierwave文件上传,和PaperTrail进行版本控制。
While versioning seems to work pretty well, it seems though that reifying a version doesn't play nice with Carrierwave's remove_previously_stored_files_after_update
config deactivated: in this situation, a simple reload
of the reified model instance doesn't work anymore, only an explicit fresh load from the database (using Model.find 123
) works.
虽然版本控制看起来运行得很好,但是似乎对一个版本的具体化并不适合Carrierwave的remove_previously_stored_files_after_update配置被停用:在这种情况下,一个简单的重新加载的具体化的模型实例不再工作了,只有一个来自数据库的显式的新加载(使用模型)。发现123)的作品。
I have created a demo Rails app to demonstrate the problem.
我创建了一个演示Rails应用来演示这个问题。
The User
model mounts two uploaders:
用户模型安装两个上传者:
-
AvatarUploader
, which removes the file after update (default) - AvatarUploader,在更新后删除文件(默认)
-
KeepingFilesAvatarUploader
, which doesn't remove the file after update, using theremove_previously_stored_files_after_update
option set tofalse
- KeepingFilesAvatarUploader,它不会在更新后删除文件,使用remove_previously_stored_files_after_update选项设置为false。
Code of AvatarUploader
:
AvatarUploader代码:
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{model.id}/#{mounted_as}"
end
end
Code of KeepingFilesAvatarUploader
:
KeepingFilesAvatarUploader代码:
# encoding: utf-8
class KeepingFilesAvatarUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{model.id}/#{mounted_as}"
end
configure do |config|
config.remove_previously_stored_files_after_update = false
end
end
The only difference is the remove_previously_stored_files_after_update
option.
惟一的区别是remove_previously_stored_files_after_update选项。
Here's the code of the User
model:
下面是用户模型的代码:
class User < ActiveRecord::Base
has_paper_trail only: [:name, :avatar, :keeping_files_avatar]
mount_uploader :avatar, AvatarUploader
mount_uploader :keeping_files_avatar, KeepingFilesAvatarUploader
end
I have written some specs which demonstrate the unexpected behaviour. As they are a bit long to post, see here:
我已经写了一些说明这种意外行为的规范。因为他们有点长,所以在这里看到:
https://github.com/jmuheim/test-carrierwave-papertrail/blob/master/spec/models/user_spec.rb#L20
https://github.com/jmuheim/test-carrierwave-papertrail/blob/master/spec/models/user_spec.rb活用
All specs pass except the one that I set to pending
, here's the output:
所有的规格通过,除了我设置的待定,这里是输出:
1) User versioning reloading the model after reify sets "keeping_files_avatar" to the original value
# See http://*.com/questions/29624223/papertrail-doesnt-play-nice-with-carrierwave-and-remove-previously-stored-file
Failure/Error: expect(user.keeping_files_avatar.file.filename).to eq 'original-image.jpg' # This upload field isn'! <<<FAILING LINE>>>!
expected: "original-image.jpg"
got: "new-image.jpg"
(compared using ==)
# ./spec/models/user_spec.rb:20:in `block (4 levels) in <top (required)>'
Finished in 0.20704 seconds (files took 1.51 seconds to load)
4 examples, 0 failures, 1 pending
It would be really nice to get this to work. I don't need this functionality right now, but I don't want to add any technical debt to my project (especially this one could lead to very unforeseen problems as one usually relies on simply doing a reload
on a model to refresh its attributes).
把它弄出来会很好。我现在不需要这个功能,但是我不想在我的项目中添加任何技术债务(特别是这一项可能会导致非常难以预料的问题,因为通常情况下,一个人仅仅依靠在模型上重新加载来刷新它的属性)。
1 个解决方案
#1
0
Check if this will work for you
检查一下是否适合你。
class User < ActiveRecord::Base
has_paper_trail only: [:name, :avatar, :keeping_files_avatar]
mount_uploader :avatar, AvatarUploader
mount_uploader :keeping_files_avatar, KeepingFilesAvatarUploader
skip_callback :save, :after, :remove_previously_stored_keeping_files_avatar
end
#1
0
Check if this will work for you
检查一下是否适合你。
class User < ActiveRecord::Base
has_paper_trail only: [:name, :avatar, :keeping_files_avatar]
mount_uploader :avatar, AvatarUploader
mount_uploader :keeping_files_avatar, KeepingFilesAvatarUploader
skip_callback :save, :after, :remove_previously_stored_keeping_files_avatar
end