Skip to content

Initial counter_cache implementation #393

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -162,6 +162,26 @@ child1.ancestry_path
=> ["Grandparent", "Parent", "First Child"]
```

### Counter cache

It's possible to use Rails' counter cache option for the `parent` relationship by adding the `counter_cache: true` option to `has_closure_tree`.

```ruby
class Tag < ApplicationRecord
has_closure_tree counter_cache: true
end
```

You will need to generate one additional migration:

```ruby
class AddTagsCountToTag < ActiveRecord::Migration
def change
add_column :tags, :tags_count, :integer, null: false, default: 0
end
end
```

### find_or_create_by_path

You can `find` as well as `find_or_create` by "ancestry paths".
3 changes: 2 additions & 1 deletion lib/closure_tree/has_closure_tree.rb
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@ def has_closure_tree(options = {})
:dont_order_roots,
:numeric_order,
:touch,
:with_advisory_lock
:with_advisory_lock,
:counter_cache
)

class_attribute :_ct
4 changes: 3 additions & 1 deletion lib/closure_tree/model.rb
Original file line number Diff line number Diff line change
@@ -11,7 +11,9 @@ module Model
foreign_key: _ct.parent_column_name,
inverse_of: :children,
touch: _ct.options[:touch],
optional: true)
optional: true,
counter_cache: _ct.options[:counter_cache],
)

order_by_generations = -> { Arel.sql("#{_ct.quoted_hierarchy_table_name}.generations ASC") }

2 changes: 1 addition & 1 deletion spec/support/models.rb
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ def add_destroyed_tag
class UUIDTag < ActiveRecord::Base
self.primary_key = :uuid
before_create :set_uuid
has_closure_tree dependent: :destroy, order: 'name', parent_column_name: 'parent_uuid'
has_closure_tree dependent: :destroy, order: 'name', parent_column_name: 'parent_uuid', counter_cache: true
before_destroy :add_destroyed_tag

def set_uuid
1 change: 1 addition & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
t.string "title"
t.string "parent_uuid"
t.integer "sort_order"
t.integer "uuid_tags_count", default: 0, null: false
t.timestamps null: false
end

5 changes: 5 additions & 0 deletions spec/support/tag_examples.rb
Original file line number Diff line number Diff line change
@@ -23,6 +23,11 @@
expected_parent_column_name = tag_class == UUIDTag ? 'parent_uuid' : 'parent_id'
expect(tag_class._ct.parent_column_name).to eq(expected_parent_column_name)
end

it 'should counter_cache parent relationship' do
expected_counter_cache = tag_class == UUIDTag ? true : nil
expect(tag_class._ct.options[:counter_cache]).to be expected_counter_cache
end
end

describe 'from empty db' do