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

#descendantsObject



50
51
52
# File 'lib/hierarchical_db.rb', line 50

def descendants
  self.class.where(lft: (self.lft + 1..self.rgt - 1)).order(:lft)
end

#descendants_countObject



54
55
56
# File 'lib/hierarchical_db.rb', line 54

def descendants_count
  (self.rgt - self.lft - 1)/2
end

#destroy_nodeObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hierarchical_db.rb', line 90

def destroy_node
  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_subtreeObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hierarchical_db.rb', line 103

def display_subtree
  right = []
  self.descendants.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_nodeObject



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
87
88
# File 'lib/hierarchical_db.rb', line 58

def insert_node
  father = self.parent
  previous_right = ""
  #case it has descendants
  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
      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
    end
  #case it is root
  else
    last_brother = self.class.where(:rgt => self.class.maximum(:rgt))
    unless last_brother.empty?
      last_brother = last_brother[0]
      previous_right = last_brother.rgt
    end
  end
  self.lft = previous_right + 1
  self.rgt = previous_right + 2
end

#path(joined = nil, included = false) ⇒ Object



117
118
119
120
121
122
# File 'lib/hierarchical_db.rb', line 117

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) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/hierarchical_db.rb', line 41

def sort_subtree left
  right = left + 1
  self.children.each{|child| right = child.sort_subtree(right) }
  self.lft = left
  self.rgt = right
  self.save
  return right + 1
end