From 3f89e4d5185b74433c79f26a83326ed73b4e949e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A9=20Dupuis?= Date: Thu, 7 Mar 2024 19:14:10 -0800 Subject: [PATCH] Support prebuilt counter cache association list --- lib/paranoia.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 0b0d743a..05f15b08 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -173,8 +173,25 @@ def really_destroy!(update_destroy_attributes: true) private + def counter_cache_disabled? + defined?(@_disable_counter_cache) && @_disable_counter_cache + end + + def counter_cached_association_names + return [] if counter_cache_disabled? + super + end + def each_counter_cached_associations - !(defined?(@_disable_counter_cache) && @_disable_counter_cache) ? super : [] + return [] if counter_cache_disabled? + + if defined?(super) + super + else + counter_cached_association_names.each do |name| + yield association(name) + end + end end def paranoia_restore_attributes