Skip to content

Commit 043b0bc

Browse files
author
Tom Smyth
committed
Fixed bugs with *_including_tree argument list
1 parent 59238dc commit 043b0bc

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

lib/closure_tree/has_closure_tree_root.rb

+8-10
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ def has_closure_tree_root(assoc_name, options = {})
1515
has_one assoc_name, -> { where(parent: nil) }, options
1616

1717
# Fetches the association, eager loading all children and given associations
18-
define_method("#{assoc_name}_including_tree") do |assoc_map_or_reload = nil, assoc_map = nil|
18+
define_method("#{assoc_name}_including_tree") do |*args|
1919
reload = false
20-
if assoc_map_or_reload.is_a?(::Hash)
21-
assoc_map = assoc_map_or_reload
22-
else
23-
reload = assoc_map_or_reload
24-
end
20+
reload = args.shift if args && (args.first == true || args.first == false)
21+
assoc_map = args
22+
assoc_map = [nil] if assoc_map.blank?
2523

24+
# Memoize
25+
@closure_tree_roots ||= {}
26+
@closure_tree_roots[assoc_name] ||= {}
2627
unless reload
27-
# Memoize
28-
@closure_tree_roots ||= {}
29-
@closure_tree_roots[assoc_name] ||= {}
3028
if @closure_tree_roots[assoc_name].has_key?(assoc_map)
3129
return @closure_tree_roots[assoc_name][assoc_map]
3230
end
@@ -52,7 +50,7 @@ def has_closure_tree_root(assoc_name, options = {})
5250

5351
# Fetch all descendants in constant number of queries.
5452
# This is the last query-triggering statement in the method.
55-
temp_root.self_and_descendants.includes(assoc_map).each do |node|
53+
temp_root.self_and_descendants.includes(*assoc_map).each do |node|
5654
id_hash[node.id] = node
5755
parent_node = id_hash[node[parent_col_id]]
5856

spec/db/schema.rb

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
create_table "contracts" do |t|
7272
t.integer "user_id", :null => false
7373
t.integer "contract_type_id"
74+
t.string "title"
7475
end
7576

7677
create_table "contract_types" do |t|

spec/has_closure_tree_root_spec.rb

+29-7
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,36 @@
2626
user3.children << user5
2727
user3.children << user6
2828

29-
user1.contracts.create!(contract_type: ct1)
30-
user2.contracts.create!(contract_type: ct1)
31-
user3.contracts.create!(contract_type: ct1)
32-
user3.contracts.create!(contract_type: ct2)
33-
user4.contracts.create!(contract_type: ct2)
34-
user5.contracts.create!(contract_type: ct1)
35-
user6.contracts.create!(contract_type: ct2)
29+
user1.contracts.create!(title: "Contract 1", contract_type: ct1)
30+
user2.contracts.create!(title: "Contract 2", contract_type: ct1)
31+
user3.contracts.create!(title: "Contract 3", contract_type: ct1)
32+
user3.contracts.create!(title: "Contract 4", contract_type: ct2)
33+
user4.contracts.create!(title: "Contract 5", contract_type: ct2)
34+
user5.contracts.create!(title: "Contract 6", contract_type: ct1)
35+
user6.contracts.create!(title: "Contract 7", contract_type: ct2)
3636
end
3737

3838
context "with basic config" do
3939
let!(:group) { Group.create!(name: "TheGroup") }
4040

41+
it "loads all nodes in a constant number of queries" do
42+
expect do
43+
root = group_reloaded.root_user_including_tree
44+
expect(root.children[0].email).to eq "2@example.com"
45+
expect(root.children[0].parent.children[1].email).to eq "3@example.com"
46+
end.to_not exceed_query_limit(2)
47+
end
48+
49+
it "loads all nodes plus single association in a constant number of queries" do
50+
expect do
51+
root = group_reloaded.root_user_including_tree(:contracts)
52+
expect(root.children[0].email).to eq "2@example.com"
53+
expect(root.children[0].parent.children[1].email).to eq "3@example.com"
54+
expect(root.children[0].children[0].contracts[0].user.
55+
parent.parent.children[1].children[1].contracts[0].title).to eq "Contract 7"
56+
end.to_not exceed_query_limit(3)
57+
end
58+
4159
it "loads all nodes and associations in a constant number of queries" do
4260
expect do
4361
root = group_reloaded.root_user_including_tree(contracts: :contract_type)
@@ -66,6 +84,10 @@
6684
to eq "1@example.com"
6785
end
6886

87+
it "works if true passed on first call" do
88+
expect(group_reloaded.root_user_including_tree(true).email).to eq "1@example.com"
89+
end
90+
6991
it "eager loads inverse association to group" do
7092
expect do
7193
root = group_reloaded.root_user_including_tree

0 commit comments

Comments
 (0)