如何测试是否carrierwave文件改变了吗? [英] How to test if carrierwave file changed?
问题描述
我想测试一个图形使用载波改变了我的code,但我不能找到一个方法来做到这一点。看来,对象被标记为即使该文件是无效的改变,因此它并没有真正改变。
I'm trying to test if a image was changed with carrier wave on my code, but I can't find a way to do this. It seems that the object is marked as changed even if the file is invalid, and thus it was not really changed.
看看下面的输出:
(rdb:1) job.translated_xliff
#<XliffUploader:0xcd8b380 ...>
(rdb:1) job.changed?
false # As expected, didn't changed yet
(rdb:1) job.translated_xliff = "foo"
"foo"
(rdb:1) job.changed?
true # Changed? "foo" is not a valid file. Lets see the file again...
(rdb:1) job.translated_xliff
#<XliffUploader:0xcd8b380 ...> # same object ID!
我如何检查,如果该对象是真的改变了我的code?
How can I check if this object was really changed on my code?
编辑:我开了一个问题在github ,但问题仍然没有解决,但更多的信息可以在那里找到
I opened an issue on github, but the problem still not solved, yet more information can be found there
推荐答案
在内部Carrierwave定义(重写)的ActiveRecord 的getter / setter
上载列( translated_xliff
)
Internally Carrierwave define(override) the ActiveRecord getter/setter
on uploader column (translated_xliff
)
现在,每次一个呼叫setter方法job.translated_xliff ='富'(如提及由 Bensie
),它使用的ActiveRecord ::肮脏
_will_change!
方法通知对象将其更改
Now every time one call the setter method job.translated_xliff = 'foo' (as mention by Bensie
) it uses ActiveRecord::Dirty
_will_change!
method to notify the object it changing
其次CarrierWave缓存提供的文件
followed by CarrierWave caching the supplied file
在这里你的场景
job.translated_xliff
=> uploader
job.changed?
=> false
job.translated_xliff = "foo"
## Now your trying to change the object so will_change! notify that
##'translated_xliff' is changing after which Carrierwave try to cache the given 'file'(foo)
现在这里只是Carriewave问题之前启动高速缓存的文件时,它执行基本检查像这样
Now here the problem just before Carriewave start caching the file it perform a basic check like this
new_file = CarrierWave::SanitizedFile.new(new_file)
unless new_file.empty?
## Then do caching
end
空?
是用来确定提供的价值(在你的情况下,它富
)是一个路径
或文件
和$ C>修改
empty?
is use to determine is supplied value (in your case it foo
) is a Path
or File
and it neither of two in your case and hence the file is never cache but the object is marked as changed
,因此你得到这个
job.changed?
=> true
现在作为一个结果,而调用 job.translated_xliff
方法,它拉文件
从商店自供应值
从来没有缓存,你可以看到它的工作过的这里
Now as a result while calling the job.translated_xliff
method it pull the file
from the store since the supplied value
was never cached , you can see it working over here
因此,我提了意见。如果你想实现的东西,那种也许你可以检查对标识符
,并决定是否改变了
Hence I mention in the comment If you want to achieve something of that kind perhaps you can check against the identifier
and decide whether it changed
job.translated_xliff.identifier ==富
只是一个建议,但:)
希望这有助于
这篇关于如何测试是否carrierwave文件改变了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!