Skip to content

Commit 0986c14

Browse files
committed
Properly call destroy method so after_commit callback is called.
1 parent d32840d commit 0986c14

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

lib/destroyed_at.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ def destroyed(time = nil)
3939

4040
# Set an object's destroyed_at time.
4141
def destroy(timestamp = nil)
42-
timestamp ||= @marked_for_destruction_at || current_time_from_proper_timezone
43-
raw_write_attribute(:destroyed_at, timestamp)
44-
run_callbacks(:destroy) do
45-
destroy_associations
46-
self.class.unscoped.where(self.class.primary_key => id).update_all(destroyed_at: timestamp)
47-
@destroyed = true
42+
with_transaction_returning_status do
43+
timestamp ||= @marked_for_destruction_at || current_time_from_proper_timezone
44+
raw_write_attribute(:destroyed_at, timestamp)
45+
46+
run_callbacks(:destroy) do
47+
destroy_associations
48+
self.class.unscoped.where(self.class.primary_key => id).update_all(destroyed_at: timestamp)
49+
@destroyed = true
50+
end
4851
end
4952
end
5053

test/destroyed_at_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,11 @@
260260
end
261261
end
262262

263+
describe 'destroying on object should call after_commit callback' do
264+
it 'calls after_commit callback on: :destroy' do
265+
comment = Comment.create
266+
comment.destroy
267+
268+
comment.after_commited.must_equal true
269+
end
270+
end

test/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ class Comment < ActiveRecord::Base
8080
belongs_to :commenter
8181

8282
has_many :likes, as: :likeable, dependent: :destroy
83+
84+
after_commit :foo, on: :destroy
85+
86+
attr_reader :after_commited
87+
88+
def foo
89+
@after_commited = true
90+
end
8391
end
8492

8593
class Commenter < ActiveRecord::Base

0 commit comments

Comments
 (0)