Skip to content

Commit

Permalink
Indent code examples to get proper doc code output / highlight
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
carlosantoniodasilva committed Jun 3, 2024
1 parent 40a5236 commit 060d6ca
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions activerecord/lib/active_record/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ module ActiveRecord
# After updating the database state, you may sometimes need to perform some extra work, or reflect these
# changes in a remote system like clearing or updating a cache:
#
# def publish_article(article)
# article.update!(published: true)
# NotificationService.article_published(article)
# end
# def publish_article(article)
# article.update!(published: true)
# NotificationService.article_published(article)
# end
#
# The above code works but has one important flaw, which is that it no longer works properly if called inside
# a transaction, as it will interact with the remote system before the changes are persisted:
#
# Article.transaction do
# article = create_article(article)
# publish_article(article)
# end
# Article.transaction do
# article = create_article(article)
# publish_article(article)
# end
#
# The callbacks offered by ActiveRecord::Transaction allow to rewriting this method in a way that is compatible
# with transactions:
#
# def publish_article(article)
# article.update!(published: true)
# Article.current_transaction.after_commit do
# NotificationService.article_published(article)
# def publish_article(article)
# article.update!(published: true)
# Article.current_transaction.after_commit do
# NotificationService.article_published(article)
# end
# end
# end
#
# In the above example, if +publish_article+ is called inside a transaction, the callback will be invoked
# after the transaction is successfully committed, and if called outside a transaction, the callback will be invoked
Expand Down

0 comments on commit 060d6ca

Please sign in to comment.