Skip to content
This repository was archived by the owner on Oct 5, 2018. It is now read-only.

Prevent deleting processing image while attachment is processing #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/delayed_paperclip.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'delayed_paperclip/jobs'
require 'delayed_paperclip/attachment'
require 'delayed_paperclip/storage'
require 'delayed_paperclip/url_generator'
require 'delayed_paperclip/railtie'

Expand Down
10 changes: 10 additions & 0 deletions lib/delayed_paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ module Attachment
def self.included(base)
base.send :include, InstanceMethods
base.send :attr_accessor, :job_is_processing
base.alias_method_chain :initialize, :delay
base.alias_method_chain :post_processing, :delay
base.alias_method_chain :post_processing=, :delay
base.alias_method_chain :save, :prepare_enqueueing
end

module InstanceMethods

def initialize_with_delay(*args)
initialize_without_delay(*args)

# Extend our storage module after Paperclip extends their storage
# module so we can add some functionality to the #exists? method for
# all storages and still super to their original implementation.
self.extend Storage
end

def delayed_options
options[:delayed]
end
Expand Down
17 changes: 17 additions & 0 deletions lib/delayed_paperclip/storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module DelayedPaperclip
module Storage

# If the style is processing, it doesn't exist yet. Otherwise, let
# Paperclip's storage adapter determine if it exists.
#
# Technically the job may currently be processing and have already generated
# some styles but this will continue to return false until all styles are
# finished processing.
def exists?(style_name = default_style)
return false if processing_style?(style_name)

super
end

end
end
Binary file added spec/fixtures/processing.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions spec/integration/examples/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,74 @@
end
end

describe 'destroying the model' do
it 'deletes all styles when not processing' do
reset_class "Dummy", with_processed: true,
paperclip: {
styles: {
thumbnail: '12x12'
}
}

dummy.save!
process_jobs

dummy.reload

expect(dummy.image.url(:original)).to start_with("/system/dummies/images/000/000/001/original/12k.png")
expect(dummy.image.exists?(:original)).to be_truthy
expect(dummy.image.url(:thumbnail)).to start_with("/system/dummies/images/000/000/001/thumbnail/12k.png")
expect(dummy.image.exists?(:thumbnail)).to be_truthy

dummy.destroy!

expect(dummy.image.exists?(:original)).to be_falsey
expect(dummy.image.exists?(:thumbnail)).to be_falsey
end

it 'deletes only the non-delayed styles when processing' do
# Copy the processing image into the public folder
FileUtils.mkdir_p(Pathname.new(ROOT).join('spec', 'tmp', 'public', 'images'))
FileUtils.cp(
Pathname.new(ROOT).join('spec', 'fixtures', 'processing.gif'),
Pathname.new(ROOT).join('spec', 'tmp', 'public', 'images')
)

reset_class "Dummy", with_processed: true,
only_process: [:thumbnail],
processing_image_url: '/images/processing.gif',
paperclip: {
styles: {
thumbnail: '12x12'
},
only_process: [:original]
}

dummy.save!

dummy.reload

original_path = dummy.image.path(:original)
thumbnail_path = dummy.image.path(:thumbnail)

expect(original_path).to include("/system/dummies/images/000/000/001/original/12k.png")
expect(thumbnail_path).to include("/images/processing.gif")

expect(File.exist?(original_path)).to be_truthy
expect(File.exist?(thumbnail_path)).to be_truthy

expect(dummy.image.exists?(:original)).to be_truthy
expect(dummy.image.exists?(:thumbnail)).to be_falsey

dummy.destroy!

expect(dummy.image.exists?(:original)).to be_falsey
expect(dummy.image.exists?(:thumbnail)).to be_falsey

expect(File.exist?(original_path)).to be_falsey
expect(File.exist?(thumbnail_path)).to be_truthy
end
end

end