diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c1dd592..be4dc6e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### 7.1.0 + +- Optimize the grouping behavior when finding by large path [PR 347](https://github.com/ClosureTree/closure_tree/pull/347) + ### 7.0.0 Closure Tree is now tested against Rails 5.2 diff --git a/lib/closure_tree/support.rb b/lib/closure_tree/support.rb index 24459db9..734b68f5 100644 --- a/lib/closure_tree/support.rb +++ b/lib/closure_tree/support.rb @@ -146,7 +146,7 @@ def max_join_tables def find_by_large_path(path, attributes = {}, parent_id = nil) next_parent_id = parent_id child = nil - path.in_groups(max_join_tables, false).each do |subpath| + path.in_groups_of(max_join_tables, false).each do |subpath| child = model_class.find_by_path(subpath, attributes, next_parent_id) return nil if child.nil? next_parent_id = child._ct_id diff --git a/spec/tag_examples.rb b/spec/tag_examples.rb index 14dd8cf7..ab716347 100644 --- a/spec/tag_examples.rb +++ b/spec/tag_examples.rb @@ -781,11 +781,13 @@ def assert_parent_and_children end it 'finds_by_path for very deep trees' do - expect(tag_class._ct).to receive(:max_join_tables).at_least(1).and_return(3) - path = (1..20).to_a.map { |ea| ea.to_s } + expect(tag_class._ct).to receive(:max_join_tables).at_least(1).and_return(20) + path = (1..70).to_a.map { |ea| ea.to_s } subject = tag_class.find_or_create_by_path(path) expect(subject.ancestry_path).to eq(path) - expect(tag_class.find_by_path(path)).to eq(subject) + expect(count_queries do + expect(tag_class.find_by_path(path)).to eq(subject) + end).to eq(4) root = subject.root expect(root.find_by_path(path[1..-1])).to eq(subject) end