tree_structure 0.1.0 → 0.1.2
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 +93 -9
- data/lib/tree_structure/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70f3e844938f3d7496deb0d5c4b0ebfd981afdc644a3a0df3450c6842e54af45
|
4
|
+
data.tar.gz: 9f5b1c15446c2efdb91496a8313e02ce371eafaa7f4d622442c8af23e2ada8ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45afc30b33020fc5e12dfda16c82af5a6523d96d4891c6d5fa8eb65a063bb7c68a6f8b3bb8238be3b07000b009f3060c9873989ddda4299fa5ea77a58609ebcb
|
7
|
+
data.tar.gz: 5286a86079989d464d4a76ce86dfcebb6360101a905e05a047d269d315d7ccd16acbd1796359cba4faf3283b31348594dc8117c3b705a6ffe3b680ab79738ffe
|
data/README.md
CHANGED
@@ -1,8 +1,5 @@
|
|
1
1
|
# TreeStructure
|
2
|
-
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tree_structure`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
2
|
+
Tree Structure allows the records of a ActiveRecord model to be organized in a tree structure.
|
6
3
|
|
7
4
|
## Installation
|
8
5
|
|
@@ -20,15 +17,102 @@ Or install it yourself as:
|
|
20
17
|
|
21
18
|
$ gem install tree_structure
|
22
19
|
|
23
|
-
##
|
20
|
+
## Development
|
21
|
+
|
22
|
+
After installing the `tree_structure` gem in your Gemfile, follow these steps to set up the tree structure:
|
24
23
|
|
25
|
-
|
24
|
+
1. **Add a `parent_id` column** to the table where you want to organize the tree structure. You can do this by creating a migration:
|
25
|
+
```ruby
|
26
|
+
class AddParentIdToYourModel < ActiveRecord::Migration[6.0]
|
27
|
+
def change
|
28
|
+
add_column :your_model_name, :parent_id, :integer
|
29
|
+
add_index :your_model_name, :parent_id
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
26
34
|
|
27
|
-
|
35
|
+
2. **Include the TreeStructure module** in the model that will be organized in a tree structure. For example:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class YourModelName < ApplicationRecord
|
39
|
+
include TreeStructure
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
3. **Run your migrations** to update the database schema:
|
45
|
+
```ruby
|
46
|
+
rails db:migrate
|
47
|
+
````
|
48
|
+
|
49
|
+
### Example Usage
|
50
|
+
|
51
|
+
Once you have set up your model, you can start using the tree structure features. For example:
|
52
|
+
|
53
|
+
# Create root nodes
|
54
|
+
```ruby
|
55
|
+
root1 = YourModelName.create(name: 'Root 1')
|
56
|
+
root2 = YourModelName.create(name: 'Root 2')
|
57
|
+
```
|
58
|
+
|
59
|
+
# Create child nodes
|
60
|
+
```ruby
|
61
|
+
child1 = YourModelName.create(name: 'Child 1', parent: root1)
|
62
|
+
child2 = YourModelName.create(name: 'Child 2', parent: root1)
|
63
|
+
child3 = YourModelName.create(name: 'Child 3', parent: root2)
|
64
|
+
```
|
65
|
+
# Identify root nodes
|
66
|
+
```ruby
|
67
|
+
root1.root? #true
|
68
|
+
child1.root? #false
|
69
|
+
```
|
70
|
+
|
71
|
+
# Identify leaf nodes
|
72
|
+
```ruby
|
73
|
+
root1.leaf? #false
|
74
|
+
child1.leaf? #true
|
75
|
+
```
|
76
|
+
|
77
|
+
# Find ancestors
|
78
|
+
```ruby
|
79
|
+
child1.ancestors
|
80
|
+
```
|
28
81
|
|
29
|
-
|
82
|
+
# Find descendants(will return descendants)
|
83
|
+
```ruby
|
84
|
+
root1.descendants
|
85
|
+
```
|
30
86
|
|
31
|
-
|
87
|
+
# Find subtree(will return subtree)
|
88
|
+
```ruby
|
89
|
+
root1.subtree
|
90
|
+
```
|
91
|
+
|
92
|
+
# Other nodes with same parent
|
93
|
+
```ruby
|
94
|
+
child1.siblings
|
95
|
+
```
|
96
|
+
|
97
|
+
# Root to child path
|
98
|
+
```ruby
|
99
|
+
child1.path_to_root
|
100
|
+
```
|
101
|
+
|
102
|
+
# Root to child depth
|
103
|
+
```ruby
|
104
|
+
child2.depth
|
105
|
+
```
|
106
|
+
|
107
|
+
# Nodes without children
|
108
|
+
```ruby
|
109
|
+
root1.leaves
|
110
|
+
```
|
111
|
+
|
112
|
+
# Root to child depth
|
113
|
+
```ruby
|
114
|
+
child2.move_to_child_of root1
|
115
|
+
```
|
32
116
|
|
33
117
|
## Contributing
|
34
118
|
|