<img src=“https://badge.fury.io/rb/tree_hierarchy.svg” alt=“Gem Version” />
-
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
Add the following line to your Gemfile
gem ‘tree_hierarchy’
-
Record need to have ‘id’,‘parent_id’ fields.
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
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>
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, .... .... .... .... }
: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, .... .... .... .... }
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
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.
AMP Technologies, Dheenadhayalan
Copyright © 2014 Dheenadhayalan, released under the MIT license