Background:
I'm developing Rails Application and in some models I'm using paperclip gem
to save attachments. In same models I'm using public_activity gem
to track the model changes & I've prepared Restore functionality based on that to be able to Undo changes on that model with specific conditions.
Now using paperclip option :preserve_files => true
it's simple to get the old file in case of delete
, but in case of update
, I don't know how.
Question:
public_activity gem
is already using before_action
callback and I can handle all dirty fields except Paperclip file update.
So how can I get the dirty updated file without changing my models or adding extra callbacks (because I'm using this achievement in multiple models and Undo functionality is generic).
Note:
I'm using File.exist?(paperclip_attachment.path)
to check whether the file is still exist or no, and it returns false in case of update
callback of public_activity
(I think it is same as before_update
callback).
I've did a simple way to get the dirty file with just edit Undo Functionality in public_activity
update callback:
dirty_file_path = Dir.glob(File.join(File.dirname(paper_clip_attachment.path), '*.*')).max { |a,b| File.ctime(a) <=> File.ctime(b) }
paper_clip_attachment.path
includes the path to the file, but the file itself is not yet created.
The code above just checks the container folder of the file paper_clip_attachment.path
then checks the latest created file and save it's path to be used in Undo step.