Permalink
Browse files

extract cache counter logic to one method

  • Loading branch information...
1 parent e2be6ea commit 94cd08be38dc086714be8f6cbe17e77f778e1ecc @tenderlove tenderlove committed Dec 12, 2013
Showing with 10 additions and 11 deletions.
  1. +10 −11 activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -37,23 +37,22 @@ def find_target?
!loaded? && foreign_key_present? && klass
end
- def update_counters(record)
+ def with_cache_name
counter_cache_name = reflection.counter_cache_column
-
return unless counter_cache_name && owner.persisted?
- return unless different_target? record
-
- record.class.increment_counter(counter_cache_name, record.id)
+ yield counter_cache_name
+ end
- decrement_counter counter_cache_name
+ def update_counters(record)
+ with_cache_name do |name|
+ return unless different_target? record
+ record.class.increment_counter(name, record.id)
+ decrement_counter name
+ end
end
def decrement_counters
- counter_cache_name = reflection.counter_cache_column
-
- return unless counter_cache_name && owner.persisted?
-
- decrement_counter counter_cache_name
+ with_cache_name { |name| decrement_counter name }
end
def decrement_counter counter_cache_name

0 comments on commit 94cd08b

Please sign in to comment.