Project

treeable

0.0
No commit activity in last 3 years
No release in over 3 years
Make any Rails model treeable. Any model is Treeable if it has parent chuld relation with itself
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Treeable

Any model which has belongs_to and has_many relation with itself can use this. It exposes a lot of ready to use methods.

Installation

gem install treeable	

Once the gem is installed you can include the module for any model. You will need to specify the column which holds the parent id (default: parent_id) and a column where Treeable will store the path (default: path).

class User < ApplicationRecord

	include Treeable
    
    treeable_path_column :path
    treeable_parent_column :parent_id
end

Class Methods

  • roots: Get all the root elements of this class
  • json_tree(nodes): Prints all the nodes and their children.
     p User.json_tree(User.all)
     # = > [{:id=>1, :children=>[{:id=>6, :children=>[{:id=>7, :children=>[]}]}]}, {:id=>2, :children=>[{:id=>5, :children=>[]}]}, {:id=>3, :children=>[]}, {:id=>4, :children=>[]}, {:id=>5, :children=>[]}, {:id=>6, :children=>[{:id=>7, :children=>[]}]}, {:id=>7, :children=>[]}] 
  • leaves: Get all leaf nodes

Associations

Treeable automatically defines two associations:

  • parent: belongs_to relation to same class and return the parent node
  • children: has_many relation and returns all the immediate children

Instance Methods

  • parent_map: Map over all the parent nodes and executes the block provided
        User.last.parent_map{|k| p k.id}
  • children_map: Map over all the children node and executes the block provided
        User.last.children_map{|k| p k.id}
  • leaf_nodes: Returns all the leafs from this node
  • nodes_from_root: Returns all the ancestors of the node
  • is_leaf: Returns wheather the node is leaf or not
  • is_root: Returns if the node is root or noe
  • all_children: Returns the list of all the children of the node
  • root: Find the root parent node

Authors

  • Saurav Swaroop