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
|
# File 'lib/hierarchical_db.rb', line 64
def brothers
self.class.where("lft > ? and rgt < ?",self.parent.lft, self.parent.rgt)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/hierarchical_db.rb', line 113
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
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/hierarchical_db.rb', line 129
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
72
73
74
75
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
|
# File 'lib/hierarchical_db.rb', line 72
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
68
69
70
|
# File 'lib/hierarchical_db.rb', line 68
def list_by_level lvl
self.class.where(:lvl => lvl)
end
|
#path(joined = nil, included = false) ⇒ Object
143
144
145
146
147
148
|
# File 'lib/hierarchical_db.rb', line 143
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
|