text_based_nested_set 0.0.1 → 0.0.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 801c7e1b6c9348f8d13efbde0c51d6cf76b85287
|
4
|
+
data.tar.gz: 148ad9c927c282fcd9568b97db9bdc44a474f634
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ec694a77e0dc41ff0af8c5fb97ce857bc3f61e313a5e4a8c8e8b0a31d564e5c09efbb125784d9deff3a7a93c7331059f46bdcde3d89771f03922e2ec3cb408b
|
7
|
+
data.tar.gz: 00a489336c447a164b5e3a20ba70477b917dad493fda790ed05cf005b9a9cd88380e9849aade04a31d9fef498782e99e09062c3c46f3e06fb040229ac7fa5e1c
|
data/README.md
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
# TextBasedNestedSet
|
2
2
|
|
3
3
|
this acts provides Text Based Nested Set functionality.
|
4
|
+
|
4
5
|
Text Based Nested Set is another way to implementation of an SQL Nested Set created by Trever Shick(http://threebit.net/tutorials/nestedset/varcharBasedNestedSet.html).
|
6
|
+
|
5
7
|
Compare to lft and rgt Nested Set, this implementation can updates and deletions without require all of the nodes to be updated.
|
6
8
|
|
9
|
+
The methods provided by this gem can found in spec/text_based_nested_set_spec.rb
|
10
|
+
|
7
11
|
## Installation
|
8
12
|
|
9
13
|
Add this line to your application's Gemfile:
|
@@ -22,16 +26,35 @@ Or install it yourself as:
|
|
22
26
|
|
23
27
|
## Usage
|
24
28
|
|
25
|
-
set up:
|
26
|
-
|
29
|
+
### set up:
|
30
|
+
|
31
|
+
* Add parent_id, path, position column to exsiting model
|
32
|
+
```ruby
|
27
33
|
class AddTextBasedNestedSetToDemo < ActiveRecord::Migration
|
28
34
|
def change
|
29
35
|
add_column :demos, :parent_id, :integer, default: 0
|
30
36
|
add_column :demos, :path, :string, default: '/0/'
|
31
|
-
add_column :demos, position, :integer, default: 0
|
37
|
+
add_column :demos, :position, :integer, default: 0
|
32
38
|
end
|
33
39
|
end
|
34
|
-
|
40
|
+
```
|
41
|
+
|
42
|
+
* add acts_as_text_based_nested_set method to target model
|
43
|
+
```ruby
|
44
|
+
class Demo < ActiveRecord::Base
|
45
|
+
acts_as_based_nested_set
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
### convert from awesome_nested_set
|
50
|
+
|
51
|
+
this gem provide a function to help you convert awesome_nested_set table to text based_nested_set table
|
52
|
+
|
53
|
+
* Make sure to add path and position column to target model. Then should change parent_id's default value to 0
|
54
|
+
* start up a rails console and run:
|
55
|
+
```ruby
|
56
|
+
Demo.convert_from_awesome_nested_set
|
57
|
+
```
|
35
58
|
|
36
59
|
## Contributing
|
37
60
|
|
@@ -139,6 +139,11 @@ module BeyondAlbert
|
|
139
139
|
current_class.where(id: parent_ids)
|
140
140
|
end
|
141
141
|
|
142
|
+
def self_and_ancestors
|
143
|
+
parent_ids = self.path.split('/').select {|v| v != "" && v != "0"} << self.id
|
144
|
+
current_class.where(id: parent_ids)
|
145
|
+
end
|
146
|
+
|
142
147
|
def children
|
143
148
|
children_path = self.path + self.id.to_s + '/'
|
144
149
|
current_class.where(path: children_path).order('position')
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'text_based_nested_set/model'
|
2
2
|
|
3
3
|
module BeyondAlbert #:nodoc:
|
4
|
-
|
5
|
-
|
4
|
+
module Acts #:nodoc:
|
5
|
+
module TextBasedNestedSet #:nodoc:
|
6
6
|
|
7
7
|
# this acts provides Text Based Nested Set functionality.
|
8
8
|
# Text Based Nested Set is another way to implementation of an SQL Nested Set created by Trever Shick.
|
@@ -22,11 +22,11 @@ module BeyondAlbert #:nodoc:
|
|
22
22
|
# See BeyondAlbert::Acts::TextBasedNestedSet::Model
|
23
23
|
# for a list ofclass methods and instance methods
|
24
24
|
# added to acts_as_text_based_nested_set models.
|
25
|
-
|
26
|
-
|
25
|
+
def acts_as_text_based_nested_set(options = {})
|
26
|
+
include Model
|
27
27
|
|
28
28
|
before_destroy :destroy_descendants
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
32
|
end
|
@@ -1,61 +1,61 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe 'TextBasedNestedSet' do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
4
|
+
before :each do
|
5
|
+
# test tree:
|
6
|
+
# root(1)
|
7
|
+
# |__child_1(2)
|
8
|
+
# | |__child_1_1(4)
|
9
|
+
# | | |__child_1_1_1(7)
|
10
|
+
# | |__child_1_2(5)
|
11
|
+
# | | |__child_1_2_1(8)
|
12
|
+
# | |__child_1_3(6)
|
13
|
+
# |__child_2(3)
|
14
|
+
@root = create(:category, id: 1, parent_id: 0, path: "/0/", position: 0)
|
15
|
+
@child_1 = create(:category, id: 2, name: "child_1", parent_id: 1, path: "/0/1/", position: 0)
|
16
|
+
@child_2 = create(:category, id: 3, name: "child_2", parent_id: 1, path: "/0/1/", position: 1)
|
17
|
+
@child_1_1 = create(:category, id: 4, name: "child_1_1", parent_id: 2, path: "/0/1/2/", position: 0)
|
18
|
+
@child_1_2 = create(:category, id: 5, name: "child_1_2", parent_id: 2, path: "/0/1/2/", position: 1)
|
19
|
+
@child_1_3 = create(:category, id: 6, name: "child_1_3", parent_id: 2, path: "/0/1/2/", position: 2)
|
20
|
+
@child_1_1_1 = create(:category, id: 7, name: "child_1_1_1", parent_id: 4, path: "/0/1/2/4/", position: 0)
|
21
|
+
@child_1_2_1 = create(:category, id: 8, name: "child_1_2_1", parent_id: 5, path: "/0/1/2/5/", position: 0)
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'move_to_root' do
|
25
|
+
it 'should move to the root of the tree' do
|
26
|
+
@child_1_2.move_to_root
|
27
|
+
@child_1_2.reload
|
28
|
+
@child_1_1.reload
|
29
|
+
@child_1_3.reload
|
30
|
+
@child_1_2_1.reload
|
31
|
+
expect(@child_1_2.path).to eq("/0/")
|
32
|
+
expect(@child_1_2.parent_id).to eq(0)
|
33
|
+
expect(@child_1_2.position).to eq(0)
|
34
|
+
expect(@child_1_1.position).to eq(0)
|
35
|
+
expect(@child_1_3.position).to eq(1)
|
36
|
+
expect(@child_1_2_1.path).to eq("/0/5/")
|
37
|
+
expect(@child_1.descendants).to eq([@child_1_1, @child_1_3, @child_1_1_1])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'move_to_left_of' do
|
42
|
+
it 'should move to the left of target node' do
|
43
|
+
@child_1_2.move_to_left_of(@child_2)
|
44
44
|
|
45
45
|
@child_1_2.reload
|
46
46
|
@child_2.reload
|
47
47
|
@child_1_3.reload
|
48
48
|
@child_1_2_1.reload
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
49
|
+
expect(@child_1_2.path).to eq('/0/1/')
|
50
|
+
expect(@child_1_2.position).to eq(1)
|
51
|
+
expect(@child_2.position).to eq(2)
|
52
|
+
expect(@child_1_3.position).to eq(1)
|
53
|
+
expect(@child_1_2_1.path).to eq('/0/1/5/')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'move_to_right_of' do
|
58
|
+
it 'should move to the right of target node' do
|
59
59
|
@child_1_1.move_to_right_of(@child_1)
|
60
60
|
|
61
61
|
@child_1_1.reload
|
@@ -67,11 +67,11 @@ describe 'TextBasedNestedSet' do
|
|
67
67
|
expect(@child_2.position).to eq(2)
|
68
68
|
expect(@child_1_2.position).to eq(0)
|
69
69
|
expect(@child_1_3.position).to eq(1)
|
70
|
-
|
71
|
-
|
70
|
+
end
|
71
|
+
end
|
72
72
|
|
73
|
-
|
74
|
-
|
73
|
+
describe 'move_to_child_of' do
|
74
|
+
it 'should move to the child of target node' do
|
75
75
|
@child_1_2.move_to_child_of(@child_1_1)
|
76
76
|
|
77
77
|
@child_1_2.reload
|
@@ -81,37 +81,43 @@ describe 'TextBasedNestedSet' do
|
|
81
81
|
expect(@child_1_2.position).to eq(1)
|
82
82
|
expect(@child_1_3.position).to eq(1)
|
83
83
|
expect(@child_1_2_1.path).to eq('/0/1/2/4/5/')
|
84
|
-
|
85
|
-
|
84
|
+
end
|
85
|
+
end
|
86
86
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
87
|
+
describe 'parent' do
|
88
|
+
it "should return current node's parent node" do
|
89
|
+
expect(@child_1_1.parent.name).to eq("child_1")
|
90
|
+
end
|
91
91
|
|
92
92
|
it "should return nil when current node is root node" do
|
93
93
|
expect(@root.parent).to eq(nil)
|
94
94
|
end
|
95
|
-
|
95
|
+
end
|
96
96
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
97
|
+
describe 'ancestors' do
|
98
|
+
it "should return current node's ancestors node" do
|
99
|
+
expect(@child_1_1.ancestors).to eq([@root, @child_1])
|
100
|
+
end
|
101
|
+
end
|
102
102
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
103
|
+
describe 'self_and_ancestors' do
|
104
|
+
it "should return current node's ancestors node include self" do
|
105
|
+
expect(@child_1_1.self_and_ancestors).to eq([@root, @child_1, @child_1_1])
|
106
|
+
end
|
107
|
+
end
|
108
108
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
109
|
+
describe 'children' do
|
110
|
+
it "should return current node's children node" do
|
111
|
+
expect(@child_1.children).to eq([@child_1_1, @child_1_2, @child_1_3])
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe 'descendants' do
|
116
|
+
it "should return current node's all descendants" do
|
117
|
+
expect(@child_1.descendants).to eq([@child_1_1, @child_1_2, @child_1_3, @child_1_1_1, @child_1_2_1])
|
118
|
+
expect(@root.descendants).to eq([@child_1, @child_2, @child_1_1, @child_1_2, @child_1_3, @child_1_1_1, @child_1_2_1])
|
119
|
+
end
|
120
|
+
end
|
115
121
|
|
116
122
|
describe 'right_sibling' do
|
117
123
|
it "should return current node's right sibling" do
|
@@ -133,27 +139,27 @@ describe 'TextBasedNestedSet' do
|
|
133
139
|
end
|
134
140
|
end
|
135
141
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
142
|
+
describe 'root?' do
|
143
|
+
it "should return true if current node is root node" do
|
144
|
+
expect(@root.root?).to eq(true)
|
145
|
+
end
|
140
146
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
147
|
+
it "should return false if current node is not root node" do
|
148
|
+
expect(@child_1.root?).to eq(false)
|
149
|
+
end
|
150
|
+
end
|
145
151
|
|
146
|
-
|
147
|
-
|
152
|
+
describe 'convert_from_awesome_nested_set' do
|
153
|
+
it "should convert awesome nested set to text based nested set" do
|
148
154
|
@root.update(parent_id: nil)
|
149
155
|
@root.reload
|
150
156
|
Category.convert_from_awesome_nested_set
|
151
157
|
expect(@root.path).to eq('/0/')
|
152
|
-
|
153
|
-
|
158
|
+
end
|
159
|
+
end
|
154
160
|
|
155
|
-
|
156
|
-
|
161
|
+
describe 'rebuild' do
|
162
|
+
it "should rebuild current node's all descendants, set path and position again " do
|
157
163
|
@root.descendants.each do |d|
|
158
164
|
d.update(path: nil, position: nil)
|
159
165
|
end
|
@@ -181,8 +187,8 @@ describe 'TextBasedNestedSet' do
|
|
181
187
|
expect(@child_1.path).to eq('/0/1/')
|
182
188
|
expect(@child_1.position).to eq(0)
|
183
189
|
expect(@child_1_1_1.path).to eq('/0/1/2/4/')
|
184
|
-
|
185
|
-
|
190
|
+
end
|
191
|
+
end
|
186
192
|
|
187
193
|
describe 'private destroy_descendants' do
|
188
194
|
it 'should destroy descendants when itself be destroyed' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: text_based_nested_set
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- beyondalbert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|