Skip to content

Commit

Permalink
Update counter cache correctly for STI records (#444)
Browse files Browse the repository at this point in the history
Fixes #415
  • Loading branch information
issei-m authored Feb 15, 2021
1 parent e097e4a commit 2a50230
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Unreleased version

* Keep current scope when calling `roots` [Petrik de Heus](https://github.com/p8)
* STI record now can update counter cache correctly [Issei Murasawa](http://github.com/issei-m)
* [Compare to 3.3.0](https://github.com/collectiveidea/awesome_nested_set/compare/v3.3.1...master)

3.3.1
Expand Down
4 changes: 2 additions & 2 deletions lib/awesome_nested_set/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ def update_counter_cache

# Decrease the counter for all old parents
if old_parent = self.parent
self.class.decrement_counter(acts_as_nested_set_options[:counter_cache], old_parent)
old_parent.class.decrement_counter(acts_as_nested_set_options[:counter_cache], old_parent)
end

# Increase the counter for all new parents
if new_parent = self.reload.parent
self.class.increment_counter(acts_as_nested_set_options[:counter_cache], new_parent)
new_parent.class.increment_counter(acts_as_nested_set_options[:counter_cache], new_parent)
end
end

Expand Down
21 changes: 21 additions & 0 deletions spec/awesome_nested_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,27 @@ def check_structure(entries, structure)
expect { subject }.to change { new_parent.reload.children_count }.by(1)
end
end

context "when class is an STI" do
let(:sti_subclass1) { Subclass1.create(name: "Subclass1") }
let(:sti_subclass2) { Subclass2.create(name: "Subclass2") }
let(:sti_subclass3) { Subclass2.create(name: "Subclass3") }

# https://github.com/collectiveidea/awesome_nested_set/issues/415
it 'should update counter cache of a parent' do
sti_subclass2.move_to_child_of sti_subclass1

sti_subclass1.reload
expect(sti_subclass1.children_count).to be 1

sti_subclass2.move_to_child_of sti_subclass3

sti_subclass1.reload
sti_subclass3.reload
expect(sti_subclass1.children_count).to be 0
expect(sti_subclass3.children_count).to be 1
end
end
end

describe "association callbacks on children" do
Expand Down
1 change: 1 addition & 0 deletions spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
t.column :parent_id, :integer
t.column :lft, :integer
t.column :rgt, :integer
t.column :children_count, :integer, null: false, default: 0
end

create_table :users, :force => true do |t|
Expand Down
2 changes: 1 addition & 1 deletion spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class ScopedUser < ActiveRecord::Base
end

class Superclass < ActiveRecord::Base
acts_as_nested_set
acts_as_nested_set counter_cache: :children_count
self.table_name = 'single_table_inheritance'
end

Expand Down

0 comments on commit 2a50230

Please sign in to comment.