Commit a01677f5 by Krzysiek Committed by Jon Yurek

Prevent file deletion on transaction rollback

parent 3367a541
......@@ -81,7 +81,7 @@ module Paperclip
name = @name
@klass.send(:after_save) { send(name).send(:save) }
@klass.send(:before_destroy) { send(name).send(:queue_all_for_delete) }
@klass.send(:after_destroy) { send(name).send(:flush_deletes) }
@klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) }
end
def add_paperclip_callbacks
......
......@@ -1419,6 +1419,15 @@ class AttachmentTest < Test::Unit::TestCase
@dummy.destroy
assert_file_not_exists(@path)
end
should "not be deleted when transaction rollbacks after model is destroyed" do
ActiveRecord::Base.transaction do
@dummy.destroy
raise ActiveRecord::Rollback
end
assert_file_exists(@path)
end
end
end
......@@ -35,8 +35,8 @@ class HasAttachedFileTest < Test::Unit::TestCase
assert_adding_attachment('avatar').defines_callback('before_destroy')
end
should 'define an after_destroy callback' do
assert_adding_attachment('avatar').defines_callback('after_destroy')
should 'define an after_commit callback' do
assert_adding_attachment('avatar').defines_callback('after_commit')
end
should 'define the Paperclip-specific callbacks' do
......@@ -116,7 +116,7 @@ class HasAttachedFileTest < Test::Unit::TestCase
define_method: nil,
after_save: nil,
before_destroy: nil,
after_destroy: nil,
after_commit: nil,
define_paperclip_callbacks: nil,
extend: nil,
name: 'Billy')
......
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