Skip to content

Commit c27915a

Browse files
committed
acts_as_recursive_tree-26 Add include options for preload_tree
1 parent 1ddb3f8 commit c27915a

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### NEXT
2+
- added __includes:__ option to __preload_tree__
3+
14
### Version 3.2.0
25
- Added #preload_tree method to preload the parent/child relations of a single node
36

lib/acts_as_recursive_tree/model.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ def leaf?
9494
#
9595
# Fetches all descendants of this node and assigns the parent/children associations
9696
#
97-
def preload_tree
98-
ActsAsRecursiveTree::Preloaders::Descendants.new(self).preload!
97+
# @param includes [Array|Hash] pass the same arguments that should be passed to the #includes() method.
98+
#
99+
def preload_tree(includes: nil)
100+
ActsAsRecursiveTree::Preloaders::Descendants.new(self, includes: includes).preload!
99101
true
100102
end
101103

@@ -107,27 +109,27 @@ def base_class
107109

108110
module ClassMethods
109111
def self_and_ancestors_of(ids, &block)
110-
Builders::Ancestors.build(self, ids, &block)
112+
ActsAsRecursiveTree::Builders::Ancestors.build(self, ids, &block)
111113
end
112114

113115
def ancestors_of(ids, &block)
114-
Builders::Ancestors.build(self, ids, exclude_ids: true, &block)
116+
ActsAsRecursiveTree::Builders::Ancestors.build(self, ids, exclude_ids: true, &block)
115117
end
116118

117119
def roots_of(ids)
118120
self_and_ancestors_of(ids).roots
119121
end
120122

121123
def self_and_descendants_of(ids, &block)
122-
Builders::Descendants.build(self, ids, &block)
124+
ActsAsRecursiveTree::Builders::Descendants.build(self, ids, &block)
123125
end
124126

125127
def descendants_of(ids, &block)
126-
Builders::Descendants.build(self, ids, exclude_ids: true, &block)
128+
ActsAsRecursiveTree::Builders::Descendants.build(self, ids, exclude_ids: true, &block)
127129
end
128130

129131
def leaves_of(ids, &block)
130-
Builders::Leaves.build(self, ids, &block)
132+
ActsAsRecursiveTree::Builders::Leaves.build(self, ids, &block)
131133
end
132134
end
133135
end

lib/acts_as_recursive_tree/preloaders/descendants.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ module Preloaders
77
# based on the preloaded data. After this, calling #parent or #children will not trigger a database query.
88
#
99
class Descendants
10-
def initialize(node)
11-
@node = node
10+
def initialize(node, includes: nil)
11+
@node = node
1212
@parent_key = node._recursive_tree_config.parent_key
13+
@includes = includes
1314
end
1415

1516
def preload!
@@ -19,7 +20,11 @@ def preload!
1920
private
2021

2122
def records
22-
@records ||= @node.descendants.to_a
23+
@records ||= begin
24+
descendants = @node.descendants
25+
descendants = descendants.includes(*@includes) if @includes
26+
descendants.to_a
27+
end
2328
end
2429

2530
def apply_records(parent_node)

spec/acts_as_recursive_tree/preloaders/descendants_spec.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,40 @@
55
RSpec.describe ActsAsRecursiveTree::Preloaders::Descendants do
66
include TreeMethods
77

8-
let(:preloader) { described_class.new(root.reload) }
9-
let(:root) { create_tree(2) }
8+
let(:preloader) { described_class.new(root.reload, includes: included_associations) }
9+
let(:included_associations) { nil }
10+
let(:root) { create_tree(2, create_node_info: true) }
1011
let(:children) { root.children }
1112

1213
describe '#preload! will set the associations target attribute' do
1314
before do
1415
preloader.preload!
1516
end
1617

17-
it 'sets the children assoction' do
18+
it 'sets the children association' do
1819
children.each do |child|
1920
expect(child.association(:children).target).not_to be_nil
2021
end
2122
end
2223

23-
it 'sets the parent assoction' do
24+
it 'sets the parent association' do
2425
children.each do |child|
2526
expect(child.association(:parent).target).not_to be_nil
2627
end
2728
end
2829
end
30+
31+
describe '#preload! will include associations' do
32+
let(:included_associations) { :node_info }
33+
34+
before do
35+
preloader.preload!
36+
end
37+
38+
it 'sets the children association' do
39+
children.each do |child|
40+
expect(child.association(included_associations).target).not_to be_nil
41+
end
42+
end
43+
end
2944
end

spec/support/tree_methods.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
# Helper methods for simple tree creation
44
module TreeMethods
5-
def create_tree(max_level, current_level = 0, node = nil)
5+
def create_tree(max_level, current_level = 0, node = nil, create_node_info: false)
66
node = Node.create!(name: 'root') if node.nil?
77

88
1.upto(max_level - current_level) do |index|
99
child = node.children.create!(name: "child #{index} - level #{current_level}")
10+
child.create_node_info if create_node_info
1011
create_tree(max_level, current_level + 1, child)
1112
end
1213

0 commit comments

Comments
 (0)