6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/closure_tree/has_closure_tree_root.rb', line 6
def has_closure_tree_root(assoc_name, options = {})
options.assert_valid_keys(
:class_name,
:foreign_key
)
options[:class_name] ||= assoc_name.to_s.sub(/\Aroot_/, "").classify
options[:foreign_key] ||= self.name.underscore << "_id"
has_one assoc_name, -> { where(parent: nil) }, options
define_method("#{assoc_name}_including_tree") do |assoc_map_or_reload = nil, assoc_map = nil|
reload = false
if assoc_map_or_reload.is_a?(::Hash)
assoc_map = assoc_map_or_reload
else
reload = assoc_map_or_reload
end
unless reload
@closure_tree_roots ||= {}
@closure_tree_roots[assoc_name] ||= {}
if @closure_tree_roots[assoc_name].has_key?(assoc_map)
return @closure_tree_roots[assoc_name][assoc_map]
end
end
roots = options[:class_name].constantize.where(parent: nil, options[:foreign_key] => id).to_a
return nil if roots.empty?
if roots.size > 1
raise MultipleRootError.new("#{self.class.name}: has_closure_tree_root requires a single root")
end
temp_root = roots.first
root = nil
id_hash = {}
parent_col_id = temp_root.class._ct.options[:parent_column_name]
inverse = temp_root.class.reflections.values.detect do |r|
r.macro == :belongs_to && r.klass == self.class
end
temp_root.self_and_descendants.includes(assoc_map).each do |node|
id_hash[node.id] = node
parent_node = id_hash[node[parent_col_id]]
parent_assoc = node.association(:parent)
parent_assoc.loaded!
parent_assoc.target = parent_node
children_assoc = node.association(:children)
children_assoc.loaded!
if parent_node
parent_node.association(:children).target << node
else
root = node
end
if inverse
inverse_assoc = node.association(inverse.name)
inverse_assoc.loaded!
inverse_assoc.target = self
end
end
@closure_tree_roots[assoc_name][assoc_map] = root
end
end
|