Commit 255a166d by Sergey Alekseev

fix `test_counter_cache_column_update_on_destroy` for Rails 4.2

This is a workaround actually. Since
`ActiveRecord::CounterCache.each_counter_cached_associations` is
private. We shouldn’t use it. Proposals for the proper fix are welcome.

The reason the previous version stopped working is `affected_rows`
returns `0` at
[active_record/counter_cache.rb#L142](https://github.com/rails/rails/blo
b/ef99d4cd3ecc58a8c1484740b2fb5447dbda23ab/activerecord/lib/active_recor
d/counter_cache.rb#L142). If you follow the method call you’ll find
that it’s called at
[active_record/persistence.rb#L486](https://github.com/rails/rails/blob/
ef99d4cd3ecc58a8c1484740b2fb5447dbda23ab/activerecord/lib/active_record/
persistence.rb#L486). Probably we’d better override the return value in
`ActiveRecord::Relation.delete_all` at
[active_record/relation.rb#L481](https://github.com/rails/rails/blob/ef9
9d4cd3ecc58a8c1484740b2fb5447dbda23ab/activerecord/lib/active_record/rel
ation.rb#L481).
parent 5b29d8b6
......@@ -69,6 +69,32 @@ module Paranoia
touch_paranoia_column
end
end
if callbacks_result
if ActiveRecord::VERSION::STRING >= '4.2'
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
self
else
false
end
end
# As of Rails 4.1.0 +destroy!+ will no longer remove the record from the db
# unless you touch the paranoia column before.
# We need to override it here otherwise children records might be removed
# when they shouldn't
if ActiveRecord::VERSION::STRING >= "4.1"
def destroy!
destroyed? ? super : destroy || raise(ActiveRecord::RecordNotDestroyed)
end
end
def delete
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment