Skip to content
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

join default_tree_scope only when a limit_depth is given #440

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion lib/closure_tree/hash_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def hash_tree(options = {})
# There is no default depth limit. This might be crazy-big, depending
# on your tree shape. Hash huge trees at your own peril!
def hash_tree(options = {})
_ct.hash_tree(_ct.default_tree_scope(all, options[:limit_depth]))
if options[:limit_depth]
_ct.hash_tree(_ct.default_tree_scope(all, options[:limit_depth]))
else
_ct.hash_tree(all)
end
end
end
end
Expand Down
22 changes: 20 additions & 2 deletions test/support/tag_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,19 @@ def assert_parent_and_children
assert_equal @full_tree, @tag_class.hash_tree(limit_depth: 4)
end

it 'joins the default scope when a limit_depth is given' do
queries = sql_queries { @tag_class.hash_tree(limit_depth: 2) }
assert queries.any? { |query| query.match?(%r{INNER JOIN \( SELECT descendant_id, MAX\(generations\) as depth}) }
end

it 'no limit' do
assert_equal @full_tree, @tag_class.hash_tree
end

it 'does not join the default scope when there is no limit' do
queries = sql_queries { @tag_class.hash_tree }
assert_equal queries.any? { |query| query.match?(%r{INNER JOIN \( SELECT descendant_id, MAX\(generations\) as depth}) }, false
end
end

describe '.hash_tree' do
Expand Down Expand Up @@ -805,6 +815,16 @@ def assert_parent_and_children
assert_equal @full_tree[@a], @a.children.hash_tree
end

it 'joins the default scope when a limit_depth is given' do
queries = sql_queries { @a.self_and_descendants.hash_tree(limit_depth: 2) }
assert queries.any? { |query| query.match?(%r{INNER JOIN \( SELECT descendant_id, MAX\(generations\) as depth}) }
end

it 'does not join the default scope when there is no limit' do
queries = sql_queries { @a.self_and_descendants.hash_tree }
assert_equal queries.any? { |query| query.match?(%r{INNER JOIN \( SELECT descendant_id, MAX\(generations\) as depth}) }, false
end

it 'limit_depth 3 from b.parent' do
assert_equal @three_tree.slice(@a), @b.parent.hash_tree(limit_depth: 3)
end
Expand Down Expand Up @@ -899,8 +919,6 @@ def assert_parent_and_children
@c3 = @tag_class.find_or_create_by_path %w[a3 b3 c3]
@b3 = @c3.parent
@a3 = @b3.parent


end

it 'should return 0 for root' do
Expand Down
Loading