Skip to content

Commit

Permalink
Merge pull request #33 from kpandya91/master
Browse files Browse the repository at this point in the history
Update counter-cache on soft delete and undelete
  • Loading branch information
grosser committed Sep 21, 2017
2 parents 76afe7a + 2a18ffa commit 128812d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/soft_deletion/core.rb
Expand Up @@ -94,12 +94,30 @@ def soft_delete_dependencies

protected

if ActiveRecord::VERSION::MAJOR < 4
def each_counter_cached_associations
reflections.each do |name, reflection|
yield association(name.to_sym) if reflection.belongs_to? && reflection.counter_cache_column
end
end
end

def update_soft_delete_counter_caches(value)
each_counter_cached_associations do |association|
if association.target
target = association.target
target.class.update_counters(target.id, association.reflection.counter_cache_column => value)
end
end
end

def _run_soft_delete(&block)
result = false
internal = lambda do
mark_as_deleted
soft_delete_dependencies.each(&:soft_delete!)
result = block.call
update_soft_delete_counter_caches(-1)
end

self.class.transaction do
Expand All @@ -118,6 +136,7 @@ def _run_soft_undelete(&block)
mark_as_undeleted
soft_delete_dependencies.each { |m| m.soft_undelete!(limit)}
result = block.call
update_soft_delete_counter_caches(1)
end

self.class.transaction do
Expand Down
29 changes: 28 additions & 1 deletion spec/soft_deletion_spec.rb
Expand Up @@ -2,7 +2,7 @@

SingleCov.covered!
SingleCov.covered! file: 'lib/soft_deletion/setup.rb'
SingleCov.covered! file: 'lib/soft_deletion/core.rb', uncovered: 2 # AR version if/else
SingleCov.covered! file: 'lib/soft_deletion/core.rb', uncovered: 5 # AR version if/else
SingleCov.covered! file: 'lib/soft_deletion/dependency.rb'

describe SoftDeletion do
Expand Down Expand Up @@ -626,4 +626,31 @@ def undelete!
end
end
end

context "counter-caches" do
it "updates counter-cache on soft delete" do
category = CCCategory.create!

forum1 = category.forums.create!
forum2 = category.forums.create!

expect(category.reload.forums_count).to eq 2

forum1.soft_delete!

expect(category.reload.forums_count).to eq 1
end

it "updates counter-cache on soft undelete" do
category = CCCategory.create!

forum1 = category.forums.create!
forum2 = category.forums.create!
forum1.soft_delete!
expect(category.reload.forums_count).to eq 1

forum1.soft_undelete!
expect(category.reload.forums_count).to eq 2
end
end
end
22 changes: 22 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -46,6 +46,11 @@ def clear_callbacks(model, callback)

create_table :original_categories do |t|
end

create_table :counter_cache_categories do |t|
t.integer :forums_count, null: false, default: 0
t.timestamp :deleted_at
end
end

# setup models
Expand Down Expand Up @@ -185,3 +190,20 @@ class Cat2ForumChild < Cat2Forum
self.table_name = 'forums'
end

# Counter cache Forum
class CCForum < ActiveRecord::Base
self.table_name = 'forums'

has_soft_deletion

belongs_to :category, class_name: 'CCCategory', foreign_key: 'category_id', counter_cache: :forums_count
end

# Counter cache category
class CCCategory < ActiveRecord::Base
self.table_name = 'counter_cache_categories'

has_soft_deletion

has_many :forums, class_name: 'CCForum', primary_key: 'id', foreign_key: 'category_id'
end

0 comments on commit 128812d

Please sign in to comment.