Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'ca-touch-commit-callbacks'

Make AR::Base#touch fire the after_commit and after_rollback callbacks.

Closes #12031.

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/test/cases/integration_test.rb
  • Loading branch information...
commit 0a66a479b31c19182a898c30e04d49c547829621 1 parent fd2d144
@carlosantoniodasilva carlosantoniodasilva authored
View
4 activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Make `touch` fire the `after_commit` and `after_rollback` callbacks.
+
+ *Harry Brundage*
+
* Don't try to get the subclass if the inheritance column doesn't exist
The `subclass_from_attrs` method is called even if the column specified by
View
4 activerecord/lib/active_record/transactions.rb
@@ -275,6 +275,10 @@ def save!(*) #:nodoc:
with_transaction_returning_status { super }
end
+ def touch(*) #:nodoc:
+ with_transaction_returning_status { super }
+ end
+
# Reset id and @new_record if the transaction rolls back.
def rollback_active_record_state!
remember_transaction_record_state
View
18 activerecord/test/cases/base_test.rb
@@ -26,6 +26,8 @@
require 'models/bird'
require 'models/car'
require 'models/bulb'
+require 'models/owner'
+require 'models/pet'
require 'rexml/document'
class FirstAbstractClass < ActiveRecord::Base
@@ -75,7 +77,8 @@ def setup
end
class BasicsTest < ActiveRecord::TestCase
- fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts
+ fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics,
+ 'warehouse-things', :authors, :categorizations, :categories, :posts, :owners, :pets
def setup
ActiveRecord::Base.time_zone_aware_attributes = false
@@ -1493,13 +1496,14 @@ def test_cache_key_format_for_existing_record_with_updated_at_and_custom_cache_t
end
def test_cache_key_changes_when_child_touched
- car = Car.create
- Bulb.create(car: car)
+ owner = owners(:blackbeard)
+ pet = pets(:parrot)
- key = car.cache_key
- car.bulb.touch
- car.reload
- assert_not_equal key, car.cache_key
+ owner.update_column :updated_at, Time.current
+ key = owner.cache_key
+
+ assert pet.touch
+ assert_not_equal key, owner.reload.cache_key
end
def test_cache_key_format_for_existing_record_with_nil_updated_timestamps
View
47 activerecord/test/cases/transaction_callbacks_test.rb
@@ -1,9 +1,11 @@
require "cases/helper"
+require 'models/owner'
+require 'models/pet'
require 'models/topic'
class TransactionCallbacksTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
- fixtures :topics
+ fixtures :topics, :owners, :pets
class ReplyWithCallbacks < ActiveRecord::Base
self.table_name = :topics
@@ -120,6 +122,18 @@ def test_only_call_after_commit_on_create_after_transaction_commits_for_new_reco
assert_equal [], reply.history
end
+ def test_only_call_after_commit_on_update_after_transaction_commits_for_existing_record_on_touch
+ @first.after_commit_block(:create){|r| r.history << :commit_on_create}
+ @first.after_commit_block(:update){|r| r.history << :commit_on_update}
+ @first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
+ @first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
+ @first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
+ @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
+
+ @first.touch
+ assert_equal [:commit_on_update], @first.history
+ end
+
def test_call_after_rollback_after_transaction_rollsback
@first.after_commit_block{|r| r.history << :after_commit}
@first.after_rollback_block{|r| r.history << :after_rollback}
@@ -148,6 +162,22 @@ def test_only_call_after_rollback_on_update_after_transaction_rollsback_for_exis
assert_equal [:rollback_on_update], @first.history
end
+ def test_only_call_after_rollback_on_update_after_transaction_rollsback_for_existing_record_on_touch
+ @first.after_commit_block(:create){|r| r.history << :commit_on_create}
+ @first.after_commit_block(:update){|r| r.history << :commit_on_update}
+ @first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
+ @first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
+ @first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
+ @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
+
+ Topic.transaction do
+ @first.touch
+ raise ActiveRecord::Rollback
+ end
+
+ assert_equal [:rollback_on_update], @first.history
+ end
+
def test_only_call_after_rollback_on_destroy_after_transaction_rollsback_for_destroyed_record
@first.after_commit_block(:create){|r| r.history << :commit_on_create}
@first.after_commit_block(:update){|r| r.history << :commit_on_update}
@@ -279,6 +309,21 @@ def test_after_rollback_callbacks_should_validate_on_condition
def test_after_commit_callbacks_should_validate_on_condition
assert_raise(ArgumentError) { Topic.send(:after_commit, :on => :save) }
end
+
+ def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_call_callbacks_on_the_parent_object
+ pet = Pet.first
+ owner = pet.owner
+ flag = false
+
+ owner.on_after_commit do
+ flag = true
+ end
+
+ pet.name = "Fluffy the Third"
+ pet.save
+
+ assert flag
+ end
end
class CallbacksOnMultipleActionsTest < ActiveRecord::TestCase
View
1  activerecord/test/models/car.rb
@@ -15,7 +15,6 @@ class Car < ActiveRecord::Base
scope :incl_engines, -> { includes(:engines) }
scope :order_using_new_style, -> { order('name asc') }
-
end
class CoolCar < Car
View
17 activerecord/test/models/owner.rb
@@ -2,4 +2,21 @@ class Owner < ActiveRecord::Base
self.primary_key = :owner_id
has_many :pets, -> { order 'pets.name desc' }
has_many :toys, :through => :pets
+
+ after_commit :execute_blocks
+
+ def blocks
+ @blocks ||= []
+ end
+
+ def on_after_commit(&block)
+ blocks << block
+ end
+
+ def execute_blocks
+ blocks.each do |block|
+ block.call(self)
+ end
+ @blocks = []
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.