Tree Hierarchy <img src=“https://badge.fury.io/rb/tree_hierarchy.svg” alt=“Gem Version” /> <img src=“https://gemnasium.com/dheenaINDIAN/tree_hierarchy.svg” alt=“Dependency Status” /> <img src=“https://codeclimate.com/github/dheenaINDIAN/tree_hierarchy.png” />¶ ↑
-
Create a Tree Structure with parent-child relationship
-
Much faster for creating tree hierarchy with large data
-
Update the Parent value with sum of its child values
Sample Output¶ ↑
{
"id"=>1,
"parent_id"=>nil,
"budget"=>25, ---> (15+10)sum of child value
: level=>1, ---> (1) 1rd level
: children=>[ ---> children line items
{
"id"=>2,
"parent_id"=>1,
"budget"=>15, ---> (15) sum of child value
:level=>2, ---> (2) 2nd level
: children=>[
{
"id"=>4,
"parent_id"=>2,
"budget"=>15,
:level=>3,
:children=>nil
}]
},
{
"id"=>5,
"parent_id"=>1,
"budget"=>10,
:level=>2
:children=>nil
}]
}
Installation :¶ ↑
Add the following line to your Gemfile
gem ‘tree_hierarchy’
Requirement :¶ ↑
-
Record need to have ‘id’,‘parent_id’ fields with same class.
Options: ¶ ↑
most_parent.hierarchy(result) # create a tree structure
where most_parent - initial level
result - result_set which contain all the records in linear form
most_parent.hierarchy_level(result) # tree structure with level specifier
:level field is merged with the record to specify the level
most_parent.hierarchy(result,depth) # define the depth (1..n)
most_parent.hierarchy_level(result,depth) # depth with level specifier
most_parent.added_hierarchy(result,['field1','field2','field3'],depth)
most_parent.added_hierarchy_level(result,['field1','field2','field3'],depth)
#updating the parent value with its child value
#field1..fieldn specifies the field name
#pass the field name in Array
most_parent.linear(result) #rearrange the result in order
most_parent.linear_level(result) # Order the result with level specifier
most_parent.added_linear_level(result) #Order the result with adding the child value
parent.hchildren(result) #returns the children record
parent.added_hchildren(result,['field1','field2']) #returns the children with adding the child value
parent.hchildren_ids(result) #returns the children ids
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=>[ ---> children hierarchy created
{
"id"=>2,
"parent_id"=>1,
"name"=>"Men",
"value1"=>0,
"value2"=>0
: children=>[ ---> children hierarchy created
{
"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, ---> level specifier added
: children=>[
{
"id"=>2,
"parent_id"=>1,
"name"=>"Men",
"value1"=>0,
"value2"=>0
:level =>2, ---> denotes the level
: 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,['value1','value2',...value-n],depth)
most_parent.added_hierarchy(record,['value1','value2'])
# value1,value2 are field names,pass as Array
output
{
"id"=>1,
"parent_id"=>nil,
"name"=>"Clothing",
"value1"=>130, ----> Sum of children's field(value1)
"value2"=>204
: children=>[
{
"id"=>2,
"parent_id"=>1,
"name"=>"Men",
"value1"=>68, ----> sum of children (value1)
"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,['value1','value2'],2)
#Created with :level field (level specifier)
Children of respective parent record¶ ↑
The child records are collected for the respective parent item.
parent.hchildren(record) output #<Categoryid: 2,parent_id: 1,name: "Men",value1: 0,value2: 0>, #<Categoryid: 3,parent_id: 1,name: "Women",value1: 0,value2: 0>
returns the children of the respective parent item
parent.added_hchildren(record,['value1']) output #<Categoryid: 2,parent_id: 1,name: "Men",value1: 45,value2: 0>, #<Categoryid: 3,parent_id: 1,name: "Women",value1: 65,value2: 0>
returns the children after updating its child value to its parent
Ordering the result_set¶ ↑
Order the records as parent,sub-child,child
Sample Order: parent-1 sub-child-2 child-3 sub-child-2 child3 most_parent.linear(record) # result in order #<Categoryid: 1,parent_id: nil,name: "Clothing",value1: 0,value2: 0>, #<Categoryid: 2,parent_id: 1,name: "Men",value1: 0,value2: 0>, #<Categoryid: 4,parent_id: 2,name: "T-Shirt",value1: 45,value2: 34>, #<Categoryid: 5,parent_id: 2,name: "Pants",value1: 23,value2: 56> #<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>, most_parent.linear_level(record) # return with level specifier and order #<Categoryid: 1,parent_id: nil,name: "Clothing",level: 1,value1: 0,value2: 0>, #<Categoryid: 2,parent_id: 1,name: "Men",level: 2,value1: 0,value2: 0>, #<Categoryid: 4,parent_id: 2,name: "T-Shirt",level: 3,value1: 45,value2: 34>, #<Categoryid: 5,parent_id: 2,name: "Pants",level: 3,value1: 23,value2: 56> #<Categoryid: 3,parent_id: 1,name: "Women",level: 2,value1: 0,value2: 0>, #<Categoryid: 6,parent_id: 3,name: "Sarees",level: 3,value1: 40,value2: 80>, #<Categoryid: 7,parent_id: 3,name: "Legins",level: 3,value1: 22,value2: 34>, most_parent.added_linear_level(record) # return with adding the child updating to its parent
Contributors¶ ↑
Dheenadhayalan
Copyright © 2014 Dheenadhayalan, released under the MIT license