Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/destroyed_at.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def destroyed(time = nil)

# Set an object's destroyed_at time.
def destroy(timestamp = nil)
transaction do
with_transaction_returning_status do
timestamp ||= @marked_for_destruction_at || current_time_from_proper_timezone
raw_write_attribute(:destroyed_at, timestamp)

Expand Down
8 changes: 8 additions & 0 deletions test/destroyed_at_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,11 @@
end
end

describe 'destroying on object should call after_commit callback' do
it 'calls after_commit callback on: :destroy' do
comment = Comment.create
comment.destroy

comment.after_committed.must_equal true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spec does not demonstrate that destroying objects fires the after_commit callback. Indeed, it will still pass if line 266 is removed altogether. This is because the #create also runs the after_commit callbacks, so the flag is already set before #destroy is even run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, but I have after_commit with on: :destroy option. It shouldn't be called on create, should it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I overlooked that; you are absolutely correct.

deletes original comment

end
end
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ class Comment < ActiveRecord::Base
belongs_to :commenter

has_many :likes, as: :likeable, dependent: :destroy

after_commit :set_after_committed_flag, on: :destroy

attr_reader :after_committed

def set_after_committed_flag
@after_committed = true
end
end

class Commenter < ActiveRecord::Base
Expand Down