Skip to content

cache_child_count added using counter_cache of belongs_to :children #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/closure_tree/acts_as_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def acts_as_tree(options = {})
:order,
:parent_column_name,
:with_advisory_lock,
:touch
:touch,
:cache_child_count
)

class_attribute :_ct
Expand Down
3 changes: 2 additions & 1 deletion lib/closure_tree/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module Model
class_name: _ct.model_class.to_s,
foreign_key: _ct.parent_column_name,
inverse_of: :children,
touch: _ct.options[:touch]
touch: _ct.options[:touch],
counter_cache: _ct.options[:cache_child_count] ? :child_count : false

# TODO, remove when activerecord 3.2 support is dropped
attr_accessible :parent if _ct.use_attr_accessible?
Expand Down
30 changes: 30 additions & 0 deletions spec/counter_cache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

describe 'caching child count' do
before do
@root = MenuItem.create
end

describe 'cache_child_count option' do
it 'should default to 0' do
expect(@root.child_count).to eq(0)
end

it 'should keep track of the direct children as they are added and removed' do
@root.children << MenuItem.new
expect(@root.reload.child_count).to eq(1)
MenuItem.create(parent: @root)
expect(@root.reload.child_count).to eq(2)
@root.children.first.destroy
expect(@root.reload.child_count).to eq(1)
end

it 'should not count children of children' do
child_node = MenuItem.new
child_node.children << MenuItem.new
@root.children << child_node

expect(@root.reload.child_count).to eq(1)
end
end
end
2 changes: 1 addition & 1 deletion spec/db/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,5 @@ class Metal < ActiveRecord::Base
end

class MenuItem < ActiveRecord::Base
acts_as_tree(touch: true, with_advisory_lock: false)
acts_as_tree(touch: true, with_advisory_lock: false, cache_child_count: true)
end
1 change: 1 addition & 0 deletions spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
create_table 'menu_items' do |t|
t.string 'name'
t.integer 'parent_id'
t.integer 'child_count', null: false, default: 0
t.timestamps
end

Expand Down