hierarchical_db 0.1.10 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +26 -2
- data/hierarchical_db.gemspec +1 -1
- data/img/tree.png +0 -0
- data/lib/generators/templates/migration_existing.rb +1 -0
- data/lib/hierarchical_db.rb +12 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a37eb0bc5a842cbaa2ca967fbbccbbb8933bc45
|
4
|
+
data.tar.gz: d1b9f93c3b683bfdd38c94e98a807fed857c452f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd188572650b78d01f48347e085db2a7010269b905dbbedf6583a7e340ef4fea42560c0615925b2fbb7a9c7b588ee5d0609a3a80464e537d9abff8444d3947f4
|
7
|
+
data.tar.gz: f528942a23f742e79619840726fb43e2001cf94fdc4b0dc13397a23216723bf8377ea139d7e02496dffbcb01c4678007ac1316ded4a7c005927d43a9b2fb5639
|
data/README.md
CHANGED
@@ -35,16 +35,22 @@ Next into the model you have to include Hierarchies adding the code below:
|
|
35
35
|
class Territory < ActiveRecord::Base
|
36
36
|
include HierarchicalDb #we added this
|
37
37
|
belongs_to :parent_territory, class_name: 'Territory'
|
38
|
+
has_many :territories, class_name: 'Territory', foreign_key: 'parent_territory_id'
|
38
39
|
end
|
39
40
|
```
|
40
|
-
Finally we add two alias methods that are useful and necessary to deal with hierarchies:
|
41
|
+
Finally we add two alias methods that are useful and necessary to deal with hierarchies and his parent method:
|
41
42
|
```ruby
|
42
43
|
class Territory < ActiveRecord::Base
|
43
44
|
include HierarchicalDb
|
44
45
|
belongs_to :parent_territory, class_name: 'Territory'
|
46
|
+
has_many :territories, class_name: 'Territory', foreign_key: 'parent_territory_id'
|
45
47
|
# alias methods
|
46
48
|
alias_method :children, :territories #we added this
|
47
49
|
alias_method :parent, :parent_territory #we added this
|
50
|
+
|
51
|
+
def parent_key
|
52
|
+
'parent_territory_id'
|
53
|
+
end
|
48
54
|
end
|
49
55
|
```
|
50
56
|
## Usage
|
@@ -56,6 +62,17 @@ Continuing the example, if you have data inside your Territory model, then you h
|
|
56
62
|
This is a Class method and is imperative to use this after load seeds or when you first enter information inside your model. If you don't have any data inside your model this method doesn't have any sense. On the other hand, if you have data inside your model and you has never executed *sort_tree* then this gem won't work.<br>
|
57
63
|
Sort_tree initializes your tree and fills *lft* and *rgt* attributes with corresponding information.<br>
|
58
64
|
After execute this command you will able to use all useful methods below
|
65
|
+
###Example Model
|
66
|
+
For all our examples we will use territories model with the data below:
|
67
|
+
 <br>
|
68
|
+
Territory model has the attributes: name, lft, rgt, id and parent_territory_id
|
69
|
+
|
70
|
+
id | name | lft | rgt | parent_territory_id
|
71
|
+
------------ | ------------- | ------------- | ------------- | -------------
|
72
|
+
1 | Chile | 1| 8 | nil
|
73
|
+
2 | Región Metropolitana | 6| 7 | 1
|
74
|
+
3 | Región del BíoBío | 2| 5 | 1
|
75
|
+
4 | Concepción | 3| 4 | 3
|
59
76
|
###Useful Methods
|
60
77
|
|
61
78
|
#####display_tree
|
@@ -63,6 +80,13 @@ This is a Class method and returns graphically and tabulated the entire tree. Ex
|
|
63
80
|
```ruby
|
64
81
|
Territory.display_tree
|
65
82
|
```
|
83
|
+
It returns:
|
84
|
+
```
|
85
|
+
1-Chile
|
86
|
+
3-Región del BíoBío
|
87
|
+
4-Concepción
|
88
|
+
2-Región Metropolitana
|
89
|
+
```
|
66
90
|
#####descendants
|
67
91
|
Returns an array with all childrens order by lft attribute, example:
|
68
92
|
```ruby
|
@@ -83,7 +107,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
83
107
|
|
84
108
|
## Contributing
|
85
109
|
|
86
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
110
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ginzunza/hierarchical_db. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
87
111
|
|
88
112
|
|
89
113
|
## License
|
data/hierarchical_db.gemspec
CHANGED
data/img/tree.png
ADDED
Binary file
|
data/lib/hierarchical_db.rb
CHANGED
@@ -25,7 +25,7 @@ module HierarchicalDb extend ActiveSupport::Concern
|
|
25
25
|
root_nodes[0].sort_subtree(1)
|
26
26
|
else
|
27
27
|
right = 2
|
28
|
-
root_nodes.each{|n| right = n.sort_subtree(right) }
|
28
|
+
root_nodes.each{|n| right = n.sort_subtree(right, 1) }
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -35,11 +35,12 @@ module HierarchicalDb extend ActiveSupport::Concern
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
def sort_subtree left
|
38
|
+
def sort_subtree left, lvl
|
39
39
|
right = left + 1
|
40
|
-
self.children.each{|child| right = child.sort_subtree(right) }
|
40
|
+
self.children.each{|child| right = child.sort_subtree(right, lvl + 1) }
|
41
41
|
self.lft = left
|
42
42
|
self.rgt = right
|
43
|
+
self.lvl = lvl
|
43
44
|
self.save
|
44
45
|
return right + 1
|
45
46
|
end
|
@@ -56,12 +57,17 @@ module HierarchicalDb extend ActiveSupport::Concern
|
|
56
57
|
(self.rgt - self.lft - 1)/2
|
57
58
|
end
|
58
59
|
|
60
|
+
def childs
|
61
|
+
descendants.where(:lvl => self.lvl + 1)
|
62
|
+
end
|
63
|
+
|
59
64
|
def insert_node
|
60
65
|
unless self.class.is_sorted?
|
61
66
|
return
|
62
67
|
end
|
63
68
|
father = self.parent
|
64
69
|
previous_right = ""
|
70
|
+
lvl = ""
|
65
71
|
#case it has descendants
|
66
72
|
unless father.nil?
|
67
73
|
last_brother = father.descendants.where(:rgt => father.descendants.maximum(:rgt))
|
@@ -69,9 +75,11 @@ module HierarchicalDb extend ActiveSupport::Concern
|
|
69
75
|
unless last_brother.empty?
|
70
76
|
last_brother = last_brother[0]
|
71
77
|
previous_right = last_brother.rgt
|
78
|
+
lvl = last_brother.lvl
|
72
79
|
#case hasn't brothers
|
73
80
|
else
|
74
81
|
previous_right = father.rgt - 1
|
82
|
+
lvl = father.lvl + 1
|
75
83
|
end
|
76
84
|
childs = self.class.where("lft > ?", previous_right)
|
77
85
|
childs.each do |t|
|
@@ -91,6 +99,7 @@ module HierarchicalDb extend ActiveSupport::Concern
|
|
91
99
|
end
|
92
100
|
self.lft = previous_right + 1
|
93
101
|
self.rgt = previous_right + 2
|
102
|
+
self.lvl = lvl
|
94
103
|
end
|
95
104
|
|
96
105
|
def destroy_node
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hierarchical_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gustavo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- bin/console
|
56
56
|
- bin/setup
|
57
57
|
- hierarchical_db.gemspec
|
58
|
+
- img/tree.png
|
58
59
|
- lib/generators/hierarchical_db/templates/migration_existing.rb
|
59
60
|
- lib/generators/hierarchical_db_generator.rb
|
60
61
|
- lib/generators/templates/migration_existing.rb
|