From 109d1b2b10ac8b602b6a1a9995c3fd9c63aefa22 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 14 Apr 2014 14:28:26 -0400 Subject: Use inheritance chain instead of callbacks to increment counter caches after create --- .../associations/belongs_to_association.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/associations/belongs_to_association.rb') diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 8272a5584c..b0820f662a 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -31,6 +31,14 @@ module ActiveRecord @updated end + def decrement_counters + with_cache_name { |name| decrement_counter name } + end + + def increment_counters + with_cache_name { |name| increment_counter name } + end + private def find_target? @@ -51,13 +59,15 @@ module ActiveRecord end end - def decrement_counters - with_cache_name { |name| decrement_counter name } + def decrement_counter(counter_cache_name) + if foreign_key_present? + klass.decrement_counter(counter_cache_name, target_id) + end end - def decrement_counter counter_cache_name + def increment_counter(counter_cache_name) if foreign_key_present? - klass.decrement_counter(counter_cache_name, target_id) + klass.increment_counter(counter_cache_name, target_id) end end -- cgit v1.2.3 From 7af987cf297efcb0a03a168ff9486c43e0b2ff97 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 15 Apr 2014 14:21:35 -0400 Subject: Hide BelongsToAssociation#increment_counters and #decrement_counters --- activerecord/lib/active_record/associations/belongs_to_association.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/associations/belongs_to_association.rb') diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index b0820f662a..1edd4fa3aa 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -31,11 +31,11 @@ module ActiveRecord @updated end - def decrement_counters + def decrement_counters # :nodoc: with_cache_name { |name| decrement_counter name } end - def increment_counters + def increment_counters # :nodoc: with_cache_name { |name| increment_counter name } end -- cgit v1.2.3