Skip to content

ponnusamygit/tree_hierarchy

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

<img src=“https://badge.fury.io/rb/tree_hierarchy.svg” alt=“Gem Version” />

Tree Hierarchy

  1. Create a Tree Structure with parent-child relationship

  2. Much faster for creating tree hierarchy with large data

  3. Update the Parent value with sum of its child values

Installation :

Add the following line to your Gemfile

gem ‘tree_hierarchy’

Requirement :

  1. Record need to have ‘id’,‘parent_id’ fields.

Options:

most_parent.hierarchy(record)  # create a tree structure
     where most_parent - initial level
           record      - result_set which contain all the records in linear form

most_parent.hierarchy_level(record) # tree structure with level specifier 
      :level field is merged with the record to specify the level

most_parent.hierarchy(record,depth) # define the depth (1..n)
most_parent.hierarchy_level(record,depth) # depth with level specifier

most_parent.added_hierarchy(record,depth,field1,field2,field3,field4,field5)
most_parent.added_hierarchy_level(record,depth,field1,field2,field3,field4,field5)

                       #updating the parent value with its child value 
                       #field1..field5 specifies the field name
                       #pass the field name as string to field argument
                       #0 as depth for showing complete tree structure

Usage :

Shall we have Category table for our example

record = Category.all
 #<Categoryid: 1,parent_id: nil,name: "Clothing",value1: 0,value2: 0>,
 #<Categoryid: 2,parent_id: 1,name: "Men",value1: 0,value2: 0>,
 #<Categoryid: 3,parent_id: 1,name: "Women",value1: 0,value2: 0>,
 #<Categoryid: 6,parent_id: 3,name: "Sarees",value1: 40,value2: 80>,
 #<Categoryid: 7,parent_id: 3,name: "Legins",value1: 22,value2: 34>,
 #<Categoryid: 4,parent_id: 2,name: "T-Shirt",value1: 45,value2: 34>,
 #<Categoryid: 5,parent_id: 2,name: "Pants",value1: 23,value2: 56>

Create a tree structure

most_parent = record.first
most_parent.hierarchy(record)

output
 {
  "id"=>1,
  "parent_id"=>nil,
  "name"=>"Clothing",
  "value1"=>0,
  "value2"=>0
  : children=>[
      {
          "id"=>2,
          "parent_id"=>1,
          "name"=>"Men",
          "value1"=>0,
          "value2"=>0
          : children=>[
              {
                  "id"=>4,
                  "parent_id"=>2,
          .... ....
          .... .... 

   }

Create a tree_structure with level

  :level field is merged with the result set for specifying the level

 most_parent.hierarchy_level(record)

output
{
  "id"=>1,
  "parent_id"=>nil,
  "name"=>"Clothing",
  "value1"=>0,
  "value2"=>0
  :level => 1,
  : children=>[
      {
          "id"=>2,
          "parent_id"=>1,
          "name"=>"Men",
          "value1"=>0,
          "value2"=>0
          :level =>2,
          : children=>[
              {
                  "id"=>4,
                  "parent_id"=>2,
                   :level => 3,
          .... ....
          .... .... 

   }

Specify a depth level

 Tree structure created till the specified level by specifying depth      

 most_parent.hierarchy_level(record,2)  # with level field merged
 or
 most_parent.hierarchy(record,2)        # without level field merged

0 shows full tree_structure, but not necessary to mention

Adding the Child values and updating its parent

     The child values are added and updated to its sub-parent and its sum value to
its parent and the process continues till its most parent.

 most_parent.added_hierarchy(record,depth,field1,field2,field3,field4,field5)
 most_parent.added_hierarchy(record,0,'value1','value2') 
                               # value1,value2 are field names,pass as string
 output
{
   "id"=>1,
   "parent_id"=>nil,
   "name"=>"Clothing",
   "value1"=>130,
   "value2"=>204
   : children=>[
       {
           "id"=>2,
           "parent_id"=>1,
           "name"=>"Men",
           "value1"=>68,
           "value2"=>90
           : children=>[
               {
                   "id"=>4,
                   "parent_id"=>2,
           ..... ..... 
           ..... .....
  }

 The value1,value2 fields are updated with the addition of its child value

 most_parent.added_hierarchy_level(record,2,'value1','value2') 
                          #Created with :level field (level specifier) 
 Presently designed for field1 .. field5, for adding more then 5 fields 
 will be enhanced in future versions

 *Note: Should not give nil as field1 .. field5 values.

Contributors

AMP Technologies, Dheenadhayalan

Copyright © 2014 Dheenadhayalan, released under the MIT license

About

Create a tree structure and update the parent with its child values

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published