mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add after_save_commit
callback as shortcut for after_commit :hook, on: [ :create, :update ]
. (#35804)
This commit is contained in:
parent
798f175c65
commit
59bed68d5e
3 changed files with 24 additions and 0 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
* Add `after_save_commit` callback as shortcut for `after_commit :hook, on: [ :create, :update ]`.
|
||||||
|
|
||||||
|
*DHH*
|
||||||
|
|
||||||
|
* Add `ActiveRecord::Relation#extract_associated` for extracting associated records from a relation.
|
||||||
|
|
||||||
* Assign all attributes before calling `build` to ensure the child record is visible in
|
* Assign all attributes before calling `build` to ensure the child record is visible in
|
||||||
`before_add` and `after_add` callbacks for `has_many :through` associations.
|
`before_add` and `after_add` callbacks for `has_many :through` associations.
|
||||||
|
|
||||||
|
|
|
@ -234,6 +234,12 @@ module ActiveRecord
|
||||||
set_callback(:commit, :after, *args, &block)
|
set_callback(:commit, :after, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Shortcut for <tt>after_commit :hook, on: [ :create, :update ]</tt>.
|
||||||
|
def after_save_commit(*args, &block)
|
||||||
|
set_options_for_callbacks!(args, on: [ :create, :update ])
|
||||||
|
set_callback(:commit, :after, *args, &block)
|
||||||
|
end
|
||||||
|
|
||||||
# Shortcut for <tt>after_commit :hook, on: :create</tt>.
|
# Shortcut for <tt>after_commit :hook, on: :create</tt>.
|
||||||
def after_create_commit(*args, &block)
|
def after_create_commit(*args, &block)
|
||||||
set_options_for_callbacks!(args, on: :create)
|
set_options_for_callbacks!(args, on: :create)
|
||||||
|
|
|
@ -38,6 +38,7 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
before_commit { |record| record.do_before_commit(nil) }
|
before_commit { |record| record.do_before_commit(nil) }
|
||||||
after_commit { |record| record.do_after_commit(nil) }
|
after_commit { |record| record.do_after_commit(nil) }
|
||||||
|
after_save_commit { |record| record.do_after_commit(:save) }
|
||||||
after_create_commit { |record| record.do_after_commit(:create) }
|
after_create_commit { |record| record.do_after_commit(:create) }
|
||||||
after_update_commit { |record| record.do_after_commit(:update) }
|
after_update_commit { |record| record.do_after_commit(:update) }
|
||||||
after_destroy_commit { |record| record.do_after_commit(:destroy) }
|
after_destroy_commit { |record| record.do_after_commit(:destroy) }
|
||||||
|
@ -110,6 +111,17 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
|
||||||
assert_equal [:after_commit], @first.history
|
assert_equal [:after_commit], @first.history
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_only_call_after_commit_on_save_after_transaction_commits_for_saving_record
|
||||||
|
record = TopicWithCallbacks.new(title: "New topic", written_on: Date.today)
|
||||||
|
record.after_commit_block(:save) { |r| r.history << :after_save }
|
||||||
|
|
||||||
|
record.save!
|
||||||
|
assert_equal [:after_save], record.history
|
||||||
|
|
||||||
|
record.update!(title: "Another topic")
|
||||||
|
assert_equal [:after_save, :after_save], record.history
|
||||||
|
end
|
||||||
|
|
||||||
def test_only_call_after_commit_on_update_after_transaction_commits_for_existing_record
|
def test_only_call_after_commit_on_update_after_transaction_commits_for_existing_record
|
||||||
add_transaction_execution_blocks @first
|
add_transaction_execution_blocks @first
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue