Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Don't delete all the existing styles if we reprocess

  • Loading branch information...
commit e705b7376544db4daa0f423a32bae36a79fd5ac3 1 parent a596c01
@jyurek jyurek authored
View
22 lib/paperclip/attachment.rb
@@ -90,7 +90,7 @@ def assign uploaded_file
ensure_required_accessors!
file = Paperclip.io_adapters.for(uploaded_file)
- self.clear
+ self.clear(*@options[:only_process])
return nil if file.nil?
@queued_for_write[:original] = file
@@ -202,10 +202,14 @@ def save
# Clears out the attachment. Has the same effect as previously assigning
# nil to the attachment. Does NOT save. If you wish to clear AND save,
# use #destroy.
- def clear
- queue_existing_for_delete
- @queued_for_write = {}
- @errors = {}
+ def clear(*styles_to_clear)
+ if styles_to_clear.any?
+ queue_some_for_delete(*styles_to_clear)
+ else
+ queue_all_for_delete
+ @queued_for_write = {}
+ @errors = {}
+ end
end
# Destroys the attachment. Has the same effect as previously assigning
@@ -406,7 +410,13 @@ def interpolate(pattern, style_name = default_style) #:nodoc:
interpolator.interpolate(pattern, self, style_name)
end
- def queue_existing_for_delete #:nodoc:
+ def queue_some_for_delete(*styles)
+ @queued_for_delete += styles.uniq.map do |style|
+ path(style) if exists?(style)
+ end.compact
+ end
+
+ def queue_all_for_delete #:nodoc:
return if @options[:preserve_files] || !file?
@queued_for_delete += [:original, *styles.keys].uniq.map do |style|
path(style) if exists?(style)
View
2  lib/paperclip/instance_methods.rb
@@ -28,7 +28,7 @@ def destroy_attached_files
def prepare_for_destroy
Paperclip.log("Scheduling attachments for deletion.")
each_attachment do |name, attachment|
- attachment.send(:queue_existing_for_delete)
+ attachment.send(:queue_all_for_delete)
end
end
end
View
20 test/attachment_test.rb
@@ -19,6 +19,26 @@ class AttachmentTest < Test::Unit::TestCase
file.close
end
+ should "not delete styles that don't get reprocessed" do
+ file = File.new(File.join(File.dirname(__FILE__), "fixtures", "50x50.png"), 'rb')
+ rebuild_class :styles => { :small => '100x>',
+ :large => '500x>',
+ :original => '42x42#' }
+ dummy = Dummy.new
+ dummy.avatar = file
+ dummy.save
+
+ assert File.exists?(dummy.avatar.path(:small))
+ assert File.exists?(dummy.avatar.path(:large))
+ assert File.exists?(dummy.avatar.path(:original))
+
+ dummy.avatar.reprocess!(:small)
+
+ assert File.exists?(dummy.avatar.path(:small))
+ assert File.exists?(dummy.avatar.path(:large))
+ assert File.exists?(dummy.avatar.path(:original))
+ end
+
should "handle a boolean second argument to #url" do
mock_url_generator_builder = MockUrlGeneratorBuilder.new
attachment = Paperclip::Attachment.new(:name, :instance, :url_generator => mock_url_generator_builder)
Please sign in to comment.
Something went wrong with that request. Please try again.