diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-04-15 17:19:31 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-04-15 17:19:31 -0300 |
commit | e48269601b8196b285960ec6ef191624d615f9f8 (patch) | |
tree | 3a506c14fea39925a9a7ff554143fe75c29fa3b4 /activerecord/lib/active_record/counter_cache.rb | |
parent | 3870ce0677d8221ddb78bd885eafbafea6ee580e (diff) | |
parent | 7af987cf297efcb0a03a168ff9486c43e0b2ff97 (diff) | |
download | rails-e48269601b8196b285960ec6ef191624d615f9f8.tar.gz rails-e48269601b8196b285960ec6ef191624d615f9f8.tar.bz2 rails-e48269601b8196b285960ec6ef191624d615f9f8.zip |
Merge pull request #14765 from byroot/refactor-counter-cache-create-and-destroy
Refactor counter cache create and destroy
Diffstat (limited to 'activerecord/lib/active_record/counter_cache.rb')
-rw-r--r-- | activerecord/lib/active_record/counter_cache.rb | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index a5897edf03..b7b790322a 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -131,13 +131,41 @@ module ActiveRecord private + def _create_record(*) + id = super + + each_counter_cached_associations do |association| + if send(association.reflection.name) + association.increment_counters + @_after_create_counter_called = true + end + end + + id + end + def destroy_row affected_rows = super - @_actually_destroyed = affected_rows > 0 + if affected_rows > 0 + each_counter_cached_associations do |association| + foreign_key = association.reflection.foreign_key.to_sym + unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key + if send(association.reflection.name) + association.decrement_counters + end + end + end + end affected_rows end + def each_counter_cached_associations + reflections.each do |name, reflection| + yield association(name) if reflection.belongs_to? && reflection.counter_cache_column + end + end + end end |