Module: HierarchicalDb
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/hierarchical_db.rb,
lib/hierarchical_db/version.rb,
lib/generators/hierarchical_db_generator.rb
Defined Under Namespace
Modules: ClassMethods, Generators
Constant Summary
collapse
- VERSION =
"0.1.0"
Instance Method Summary
collapse
Instance Method Details
#all_nodes ⇒ Object
48
49
50
|
# File 'lib/hierarchical_db.rb', line 48
def all_nodes
self.class.where(lft: (self.lft..self.rgt)).order(:lft)
end
|
#brothers ⇒ Object
64
65
66
67
68
69
70
|
# File 'lib/hierarchical_db.rb', line 64
def brothers
unless self.parent.nil?
self.class.where("lft > ? and rgt < ? and lvl = ?",self.parent.lft, self.parent.rgt, self.lvl)
else
list_by_level 1
end
end
|
#childs ⇒ Object
60
61
62
|
# File 'lib/hierarchical_db.rb', line 60
def childs
descendants.where(:lvl => self.lvl + 1)
end
|
#descendants ⇒ Object
52
53
54
|
# File 'lib/hierarchical_db.rb', line 52
def descendants
self.class.where(lft: (self.lft + 1..self.rgt - 1)).order(:lft)
end
|
#descendants_count ⇒ Object
56
57
58
|
# File 'lib/hierarchical_db.rb', line 56
def descendants_count
(self.rgt - self.lft - 1)/2
end
|
#destroy_node ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/hierarchical_db.rb', line 117
def destroy_node
unless self.class.is_sorted?
return
end
childs = self.class.where("lft > ?", self.rgt)
childs.each do |t|
t.lft = t.lft - 2
t.save
end
childs = self.class.where("rgt > ?", self.rgt)
childs.each do |t|
t.rgt = t.rgt - 2
t.save
end
end
|
#display_subtree ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/hierarchical_db.rb', line 133
def display_subtree
right = []
self.all_nodes.each do |d|
if right.length > 0
while right.last < d.rgt do
right.pop
end
end
puts (" "*right.length) +"#{d.id}-" +d.name
right << d.rgt
end
return ''
end
|
#insert_node ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/hierarchical_db.rb', line 76
def insert_node
unless self.class.is_sorted?
return
end
father = self.parent
previous_right = ""
lvl = ""
unless father.nil?
last_brother = father.descendants.where(:rgt => father.descendants.maximum(:rgt))
unless last_brother.empty?
last_brother = last_brother[0]
previous_right = last_brother.rgt
lvl = last_brother.lvl
else
previous_right = father.rgt - 1
lvl = father.lvl + 1
end
childs = self.class.where("lft > ?", previous_right)
childs.each do |t|
t.lft = t.lft + 2
t.save
end
childs = self.class.where("rgt > ?", previous_right)
childs.each do |t|
t.rgt = t.rgt + 2
t.save
end
else
last_brother = self.class.where(:rgt => self.class.maximum(:rgt))
last_brother = last_brother[0]
previous_right = last_brother.rgt
end
self.lft = previous_right + 1
self.rgt = previous_right + 2
self.lvl = lvl
end
|
#list_by_level(lvl) ⇒ Object
72
73
74
|
# File 'lib/hierarchical_db.rb', line 72
def list_by_level lvl
self.class.where(:lvl => lvl)
end
|
#path(joined = nil, included = false) ⇒ Object
147
148
149
150
151
152
|
# File 'lib/hierarchical_db.rb', line 147
def path(joined = nil, included = false)
inc = included ? "=" : ""
nodes = self.class.where("lft <#{inc} ?", self.lft).where("rgt >#{inc} ?", self.rgt).order(:lft)
return nodes.map(&:name).join(joined) unless joined.nil?
return nodes
end
|
#sort_subtree(left, lvl) ⇒ Object
38
39
40
41
42
43
44
45
46
|
# File 'lib/hierarchical_db.rb', line 38
def sort_subtree left, lvl
right = left + 1
self.children.each{|child| right = child.sort_subtree(right, lvl + 1) }
self.lft = left
self.rgt = right
self.lvl = lvl
self.save
return right + 1
end
|