Module: Treeable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/treeable.rb
Instance Method Summary collapse
- #all_children ⇒ Object
- #all_children_ids ⇒ Object
- #children_map(&block) ⇒ Object
- #is_leaf ⇒ Object
- #is_root ⇒ Object
- #leaf_nodes ⇒ Object
- #leafs ⇒ Object
- #node_ids_from_root ⇒ Object
- #nodes_from_root ⇒ Object
- #parent_map(&block) ⇒ Object
- #path ⇒ Object
- #repopulate_path ⇒ Object
- #root ⇒ Object
- #update_path ⇒ Object
Instance Method Details
#all_children ⇒ Object
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_ids ⇒ Object
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_leaf ⇒ Object
60 61 62 |
# File 'lib/treeable.rb', line 60 def is_leaf self.children.empty? end |
#is_root ⇒ Object
64 65 66 |
# File 'lib/treeable.rb', line 64 def is_root self.parent.blank? end |
#leaf_nodes ⇒ Object
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 |
#leafs ⇒ Object
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_root ⇒ Object
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_root ⇒ Object
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 |
#path ⇒ Object
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_path ⇒ Object
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 |
#root ⇒ Object
124 125 126 |
# File 'lib/treeable.rb', line 124 def root self.path.split('.').first.to_i end |
#update_path ⇒ Object
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 |