diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-12-12 14:30:55 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-12-12 14:33:02 -0800 |
commit | 94cd08be38dc086714be8f6cbe17e77f778e1ecc (patch) | |
tree | 8b6dae3d731f745cfc31083cb6aca71d95531a6c /activerecord/lib/active_record/associations | |
parent | e2be6eacb86aa11f2c606d45a639802e946e911a (diff) | |
download | rails-94cd08be38dc086714be8f6cbe17e77f778e1ecc.tar.gz rails-94cd08be38dc086714be8f6cbe17e77f778e1ecc.tar.bz2 rails-94cd08be38dc086714be8f6cbe17e77f778e1ecc.zip |
extract cache counter logic to one method
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_association.rb | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 625a2efabd..8272a5584c 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -37,23 +37,22 @@ module ActiveRecord !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 |