Browse files

extract methods out of the cache update method

  • Loading branch information...
1 parent 6772d5e commit 9798a11c2bc9cef5d66efe5006b607c9d858da76 @tenderlove tenderlove committed Dec 12, 2013
Showing with 22 additions and 14 deletions.
  1. +22 −14 activerecord/lib/active_record/associations/belongs_to_association.rb
View
36 activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -42,20 +42,28 @@ def update_counters(record)
return unless counter_cache_name && owner.persisted?
- diff_target = if record
- different_target?(record)
- else
- owner[reflection.foreign_key]
- end
-
- if diff_target
- if record
- record.class.increment_counter(counter_cache_name, record.id)
- end
-
- if foreign_key_present?
- klass.decrement_counter(counter_cache_name, target_id)
- end
+ if record
+ update_with_record record, counter_cache_name
+ else
+ update_without_record counter_cache_name
+ end
+ end
+
+ def update_with_record record, counter_cache_name
+ return unless different_target? record
+
+ record.class.increment_counter(counter_cache_name, record.id)
+
+ decrement_counter counter_cache_name
+ end
+
+ def update_without_record counter_cache_name
+ decrement_counter counter_cache_name
+ end
+
+ def decrement_counter counter_cache_name
+ if foreign_key_present?
+ klass.decrement_counter(counter_cache_name, target_id)
end
end

0 comments on commit 9798a11

Please sign in to comment.