attachinary
attachinary copied to clipboard
destroy_file callback not invoked
I've just integrated attachinary in my project and everything is working. However when an Attachinary::File is destroyed remote file is not removed. My environment:
* rails (4.2.6)
* attachinary (1.3.1)
* cloudinary (1.1.7)
Failing test:
it 'should remove remote file', :vcr do
expect(Cloudinary.config.attachinary_keep_remote).to be_falsey
expect(item.postcard).to_not be_nil
# File should be removed
expect(item.postcard).to receive(:destroy_file).and_call_original
expect(Cloudinary::Uploader).to receive(:destroy).with(item.postcard.public_id).and_call_original
item.update!(postcard: nil)
end
In any case, I think that destroy_file should be called in an after_commit callback instead of after_destroy otherwise if for any reason transaction is aborted record is no longer valid.
I think I've found the culprit of this issue in this line. You're calling #clear which trigger a delete_all, thus callback are not invoked.
I'll see how to fix this and if I found a good solution I'll sketch a PR for this issue.
Any news about this and related PR #139??
This looks good, but I'm worried about it will break compatibility with older versions. (I don't how people with mongoid had been handling this).
Compatibility with mongoid is addressed in the if statement and old versions have a bug because they don't destroy associated file on cloudinary.