Module: Treeable

Extended by:
ActiveSupport::Concern
Defined in:
lib/treeable.rb

Instance Method Summary collapse

Instance Method Details

#all_childrenObject



102
103
104
# File 'lib/treeable.rb', line 102

def all_children
  self.class.where("#{self.class.treeable_path_column} LIKE '#{self.path}%'").where.not(:id => self.id)
end

#all_children_idsObject



106
107
108
# File 'lib/treeable.rb', line 106

def all_children_ids
    all_children.pluck(:id)
end

#children_map(&block) ⇒ Object



72
73
74
75
76
# File 'lib/treeable.rb', line 72

def children_map(&block)
  unless all_children.empty?
    children.each{|k| k.children_map(&block)}
  end  
end

#is_leafObject



60
61
62
# File 'lib/treeable.rb', line 60

def is_leaf
  self.children.empty? 
end

#is_rootObject



64
65
66
# File 'lib/treeable.rb', line 64

def is_root
  self.parent.blank?
end

#leaf_nodesObject



89
90
91
92
# File 'lib/treeable.rb', line 89

def leaf_nodes
  internal_nodes  = self.class.where(self.class.treeable_parent_column => self.all_children_ids).select(self.class.treeable_parent_column).group(self.class.treeable_parent_column).pluck(self.class.treeable_parent_column)
  self.all_children.map{|t| t unless internal_nodes.include?(t[:id])}.uniq.compact
end

#leafsObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/treeable.rb', line 78

def leafs
  array = []
  if self.is_leaf
    array << self.id
  else 
    parent_ids = self.all_children.where(self.class.treeable_parent_column => (all_children_ids << self.id)).pluck(self.class.treeable_parent_column)
    array += self.all_children_ids - parent_ids
  end
  array
end

#node_ids_from_rootObject



94
95
96
# File 'lib/treeable.rb', line 94

def node_ids_from_root
  self.path.split(".").map{|k| k.to_i}
end

#nodes_from_rootObject



98
99
100
# File 'lib/treeable.rb', line 98

def nodes_from_root
  self.class.where(:id => node_ids_from_root).order("length(#{self.class.treeable_path_column.to_s}) ASC")
end

#parent_map(&block) ⇒ Object



68
69
70
# File 'lib/treeable.rb', line 68

def parent_map(&block)
  (nodes_from_root || []).each{|k| block.call k}
end

#pathObject



54
55
56
57
58
# File 'lib/treeable.rb', line 54

def path
  if !self.attributes.keys.include?("path") && self.class.treeable_path_column !="path"
    send(self.class.treeable_path_column)
  end
end

#repopulate_pathObject



110
111
112
113
114
115
116
117
# File 'lib/treeable.rb', line 110

def repopulate_path
  if self.parent_id_changed?
    child_nodes = all_children
    path = parent ? "#{parent.path}#{self.id}." : "#{self.id}." 
    self.update_column(self.class.treeable_path_column.to_s, path)
    child_nodes.each{|k| k.update_path}
  end
end

#rootObject



124
125
126
# File 'lib/treeable.rb', line 124

def root
  self.path.split('.').first.to_i
end

#update_pathObject



119
120
121
122
# File 'lib/treeable.rb', line 119

def update_path
  new_path = self.parent ? "#{self.parent.path}#{self.id}." : "#{self.id}." 
  self.update_column(self.class.treeable_path_column.to_s, new_path)
end