Commit f820685d by Michael Dotterer Committed by Jon Yurek

Reset instance attributes when clearing attachment with preserve_files

parent 30080c4a
...@@ -233,11 +233,9 @@ module Paperclip ...@@ -233,11 +233,9 @@ module Paperclip
# nil to the attachment *and saving*. This is permanent. If you wish to # nil to the attachment *and saving*. This is permanent. If you wish to
# wipe out the existing attachment but not save, use #clear. # wipe out the existing attachment but not save, use #clear.
def destroy def destroy
unless @options[:preserve_files]
clear clear
save save
end end
end
# Returns the uploaded file if present. # Returns the uploaded file if present.
def uploaded_file def uploaded_file
...@@ -454,10 +452,12 @@ module Paperclip ...@@ -454,10 +452,12 @@ module Paperclip
end end
def queue_all_for_delete #:nodoc: def queue_all_for_delete #:nodoc:
return if @options[:preserve_files] || !file? return if !file?
unless @options[:preserve_files]
@queued_for_delete += [:original, *styles.keys].uniq.map do |style| @queued_for_delete += [:original, *styles.keys].uniq.map do |style|
path(style) if exists?(style) path(style) if exists?(style)
end.compact end.compact
end
instance_write(:file_name, nil) instance_write(:file_name, nil)
instance_write(:content_type, nil) instance_write(:content_type, nil)
instance_write(:file_size, nil) instance_write(:file_size, nil)
......
...@@ -1324,6 +1324,12 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -1324,6 +1324,12 @@ class AttachmentTest < Test::Unit::TestCase
assert_file_exists(@path) assert_file_exists(@path)
end end
should "clear out attachment data when attachment is destroyed" do
@attachment.destroy
assert !@attachment.exists?
assert_nil @dummy.avatar_file_name
end
should "not delete the file when model is destroyed" do should "not delete the file when model is destroyed" do
@dummy.destroy @dummy.destroy
assert_file_exists(@path) assert_file_exists(@path)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment