How to check if the carrier file has changed?

I'm trying to check if the carrier wave image on my code has been changed, but I cannot find a way to do this. It seems that the object is marked as modified, even if the file is invalid, and therefore it was not really changed.

See the following result:

(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! 

How to check if this object was really changed in my code?

EDIT: I opened the problem on github , but the problem is still not resolved, but more information can be found there

+4
source share
1 answer

The internal carrier wave defines (overrides) the ActiveRecord getter/setter in the loader column ( translated_xliff )

Now every time you call the setter job.translated_xliff = 'foo' method (as mentioned by Bensie ), it uses the ActiveRecord::Dirty _will_change! for notification of change of object

and then CarrierWave caches the supplied file

Here is your script

  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) 

Now here's the problem, before Carriewave starts caching the file, it performs a basic check like this

  new_file = CarrierWave::SanitizedFile.new(new_file) unless new_file.empty? ## Then do caching end 

empty? used to determine the supplied value (in your case it is foo ) is Path or file , and this is not one of the two in your case, and therefore the file is never a cache, but the object is marked as changed

and therefore you get it

 job.changed? => true 

Now, calling the job.translated_xliff method when you call job.translated_xliff , pull the file out of the repository since the provided value has never been cached, you can see that it is working on here

Therefore, I mention in the comment. If you want to achieve something like this, perhaps you can check it with an identifier and decide if it has changed

job.translated_xliff.identifier == "foo"

Just a suggestion :)

Hope for this help

+3
source

Source: https://habr.com/ru/post/1493020/


All Articles