Commit 46d9970d by Sergey Alekseev

move Rails 4.2 counter cached associations destroy inside callbacks

This will allow to detect counter cached columns changes in callbacks
like `after_destroy` or `after_commit on: :destroy`.
parent 3a48a055
...@@ -66,12 +66,8 @@ module Paranoia ...@@ -66,12 +66,8 @@ module Paranoia
def destroy def destroy
transaction do transaction do
run_callbacks(:destroy) do run_callbacks(:destroy) do
touch_paranoia_column result = touch_paranoia_column
end if result && ActiveRecord::VERSION::STRING >= '4.2'
end
if callbacks_result
if ActiveRecord::VERSION::STRING >= '4.2'
each_counter_cached_associations do |association| each_counter_cached_associations do |association|
foreign_key = association.reflection.foreign_key.to_sym foreign_key = association.reflection.foreign_key.to_sym
unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key
...@@ -81,9 +77,8 @@ module Paranoia ...@@ -81,9 +77,8 @@ module Paranoia
end end
end end
end end
self result
else end
false
end end
end end
......
...@@ -776,6 +776,19 @@ class ParanoiaTest < test_framework ...@@ -776,6 +776,19 @@ class ParanoiaTest < test_framework
# assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count # assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
end end
def test_callbacks_for_counter_cache_column_update_on_destroy
parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
related_model = parent_model_with_counter_cache_column.related_models.create
assert_equal nil, related_model.instance_variable_get(:@after_destroy_callback_called)
assert_equal nil, related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
related_model.destroy
assert related_model.instance_variable_get(:@after_destroy_callback_called)
assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
end
# TODO: find a fix for Rails 4.1 # TODO: find a fix for Rails 4.1
if ActiveRecord::VERSION::STRING !~ /\A4\.1/ if ActiveRecord::VERSION::STRING !~ /\A4\.1/
def test_counter_cache_column_update_on_really_destroy def test_counter_cache_column_update_on_really_destroy
...@@ -788,6 +801,22 @@ class ParanoiaTest < test_framework ...@@ -788,6 +801,22 @@ class ParanoiaTest < test_framework
end end
end end
# TODO: find a fix for Rails 4.0 and 4.1
if ActiveRecord::VERSION::STRING >= '4.2'
def test_callbacks_for_counter_cache_column_update_on_really_destroy!
parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
related_model = parent_model_with_counter_cache_column.related_models.create
assert_equal nil, related_model.instance_variable_get(:@after_destroy_callback_called)
assert_equal nil, related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
related_model.really_destroy!
assert related_model.instance_variable_get(:@after_destroy_callback_called)
assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
end
end
private private
def get_featureful_model def get_featureful_model
FeaturefulModel.new(:name => "not empty") FeaturefulModel.new(:name => "not empty")
...@@ -852,6 +881,19 @@ class RelatedModel < ActiveRecord::Base ...@@ -852,6 +881,19 @@ class RelatedModel < ActiveRecord::Base
acts_as_paranoid acts_as_paranoid
belongs_to :parent_model belongs_to :parent_model
belongs_to :parent_model_with_counter_cache_column, counter_cache: true belongs_to :parent_model_with_counter_cache_column, counter_cache: true
after_destroy do |model|
if parent_model_with_counter_cache_column && parent_model_with_counter_cache_column.reload.related_models_count == 0
model.instance_variable_set :@after_destroy_callback_called, true
end
end
after_commit :set_after_commit_on_destroy_callback_called, on: :destroy
def set_after_commit_on_destroy_callback_called
if parent_model_with_counter_cache_column && parent_model_with_counter_cache_column.reload.related_models_count == 0
self.instance_variable_set :@after_commit_on_destroy_callback_called, true
end
end
end end
class Employer < ActiveRecord::Base class Employer < ActiveRecord::Base
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment