Skip to content

Reference ancestor_hierarchies in depth instead of ancestors #398

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

Merged
merged 2 commits into from
Apr 11, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 8.0.0

- Drop support to EOL ruby and rails
- Reference ancestor_hierarchies in depth instead of ancestors to avoid n+1

## [7.4.0](https://github.com/ClosureTree/closure_tree/tree/7.4.0)

Expand Down
2 changes: 1 addition & 1 deletion lib/closure_tree/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def leaves
end

def depth
ancestors.size
ancestor_hierarchies.size

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code actually changes the behavior ancestor_hierarchies includes the self and ancestors does not. So this increases the result by 1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just do ancestor_hierarchies - 1 ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this change is needed at all.

Copy link
Contributor Author

@weilandia weilandia Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it so you can call depth on a node in a collection without an n+1 query (except it is off by 1 🤦).

Tag.includes(:ancestor_hierarchies).map(&:depth)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weilandia can you fix it and add a small test ? i will release a new version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end

alias_method :level, :depth
Expand Down