This fixes a few issues we've had for years. First, when attaching an image and then sending a form with validation errors, the image preview would not be rendered when the form was displayed once again. Now it's rendered as expected. Second, when attaching an image, removing it, and attaching a new one, browsers were displaying the image preview of the first one. That's because Paperclip generated the same URL from both files (as they both had the same hash data and prefix). Browsers usually cache images and render the cached image when getting the same URL. Since now we're storing each image in a different Blob, the images have different URLs and so the preview of the second one is correctly displayed. Finally, when users downloaded a document, they were getting files with a very long hexadecimal hash as filename. Now they get the original filename.
47 lines
1.3 KiB
Ruby
47 lines
1.3 KiB
Ruby
require "rails_helper"
|
|
|
|
describe "files tasks" do
|
|
describe "remove_old_cached_attachments" do
|
|
let(:run_rake_task) do
|
|
Rake::Task["files:remove_old_cached_attachments"].reenable
|
|
Rake.application.invoke_task("files:remove_old_cached_attachments")
|
|
end
|
|
|
|
it "deletes old cached attachments" do
|
|
image = build(:image)
|
|
document = build(:document)
|
|
|
|
image.storage_attachment.blob.save!
|
|
document.storage_attachment.blob.save!
|
|
|
|
travel_to(2.days.from_now) { run_rake_task }
|
|
|
|
expect(File.exists?(image.file_path)).to be false
|
|
expect(File.exists?(document.file_path)).to be false
|
|
end
|
|
|
|
it "does not delete recent cached attachments" do
|
|
image = build(:image)
|
|
document = build(:document)
|
|
|
|
image.storage_attachment.blob.save!
|
|
document.storage_attachment.blob.save!
|
|
|
|
travel_to(2.minutes.from_now) { run_rake_task }
|
|
|
|
expect(File.exists?(image.file_path)).to be true
|
|
expect(File.exists?(document.file_path)).to be true
|
|
end
|
|
|
|
it "does not delete old regular attachments" do
|
|
image = create(:image)
|
|
document = create(:document)
|
|
|
|
travel_to(2.days.from_now) { run_rake_task }
|
|
|
|
expect(File.exists?(image.file_path)).to be true
|
|
expect(File.exists?(document.file_path)).to be true
|
|
end
|
|
end
|
|
end
|