text_based_nested_set 0.0.4 → 0.0.5

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: 78749e7217cec62deb80deeead9fc78f0a772ef7
4
- data.tar.gz: 8d4c60d74da99f5713ca8fb6b1c8d01c48de47e9
3
+ metadata.gz: aa90ceee83e89edf345915734c9b5ec744fd610c
4
+ data.tar.gz: cab3873fbbbf845540707dab84d8f1227e9889e3
5
5
  SHA512:
6
- metadata.gz: baa53fc66973c9493c9e1a76fcf17ddbf478676b4ff82d098368286ea1317d80f49bae1a04b2ab5953136bfacd6d80219e03726019466e19b96fc80686964e5a
7
- data.tar.gz: 08abf2d09fea02c5b9444268b6205a1362c48f837792f9bffcdef38e718c998304cff070b29b8291b1db8c570556c67abdc386a73e7c1161524b9da3bdd8d35f
6
+ metadata.gz: fc1d0749a43faaea964cf15a84964516d23c0398a2e4615566012639999bfb6e84da134dceacad326d5ef24e8236073f7bf952098c3f6c8f1b7eedd1e1817bdf
7
+ data.tar.gz: 45e8bd6d91f6f3074d402ab9807e4f88450403a206e8b1e62ff80d965952c49faa1e1315bc5c36df41fe41afe0ded7c845942ebf4edae7dcf3f1df94e1bae03f
@@ -15,6 +15,16 @@ module BeyondAlbert
15
15
  r.rebuild
16
16
  end
17
17
  end
18
+
19
+ def position_valid?
20
+ root_nodes = where(parent_id: 0)
21
+ root_nodes.each do |r|
22
+ unless r.check_position
23
+ return false
24
+ end
25
+ end
26
+ true
27
+ end
18
28
  end
19
29
 
20
30
  def move_to_root
@@ -136,7 +146,7 @@ module BeyondAlbert
136
146
 
137
147
  def ancestors
138
148
  parent_ids = self.path.split('/').select {|v| v != "" && v != "0"}
139
- current_class.where(id: parent_ids)
149
+ current_class.where(id: parent_ids).order("LENGTH(path) ASC")
140
150
  end
141
151
 
142
152
  def self_and_ancestors
@@ -202,9 +212,25 @@ module BeyondAlbert
202
212
  end
203
213
  end
204
214
 
215
+ def check_position
216
+ logger = Logger.new(STDOUT)
217
+ if self.children.size != 0
218
+ if self.children.size == self.children.last.position + 1
219
+ self.children.each do |c|
220
+ c.check_position
221
+ end
222
+ else
223
+ logger.info("position not correct, node id: #{self.id}")
224
+ return false
225
+ end
226
+ end
227
+ return true
228
+ end
229
+
205
230
  private
206
231
 
207
232
  def in_tenacious_transaction(&block)
233
+ logger = Logger.new(STDOUT)
208
234
  retry_count = 0
209
235
  begin
210
236
  transaction(&block)
@@ -1,3 +1,3 @@
1
1
  module TextBasedNestedSet
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -21,7 +21,7 @@ describe 'TextBasedNestedSet' do
21
21
  @child_1_2_1 = create(:category, id: 3, name: "child_1_2_1", parent_id: 6, path: "/0/1/2/6/", position: 0)
22
22
  end
23
23
 
24
- describe 'move_to_root' do
24
+ describe '#move_to_root' do
25
25
  it 'should move to the root of the tree' do
26
26
  @child_1_2.move_to_root
27
27
  @child_1_2.reload
@@ -38,7 +38,7 @@ describe 'TextBasedNestedSet' do
38
38
  end
39
39
  end
40
40
 
41
- describe 'move_to_left_of' do
41
+ describe '#move_to_left_of' do
42
42
  it 'should move to the left of target node' do
43
43
  @child_1_2.move_to_left_of(@child_2)
44
44
 
@@ -54,7 +54,7 @@ describe 'TextBasedNestedSet' do
54
54
  end
55
55
  end
56
56
 
57
- describe 'move_to_right_of' do
57
+ describe '#move_to_right_of' do
58
58
  it 'should move to the right of target node' do
59
59
  @child_1_1.move_to_right_of(@child_1)
60
60
 
@@ -70,7 +70,7 @@ describe 'TextBasedNestedSet' do
70
70
  end
71
71
  end
72
72
 
73
- describe 'move_to_child_of' do
73
+ describe '#move_to_child_of' do
74
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
 
@@ -84,7 +84,7 @@ describe 'TextBasedNestedSet' do
84
84
  end
85
85
  end
86
86
 
87
- describe 'parent' do
87
+ describe '#parent' do
88
88
  it "should return current node's parent node" do
89
89
  expect(@child_1_1.parent.name).to eq("child_1")
90
90
  end
@@ -94,39 +94,39 @@ describe 'TextBasedNestedSet' do
94
94
  end
95
95
  end
96
96
 
97
- describe 'ancestors' do
97
+ describe '#ancestors' do
98
98
  it "should return current node's ancestors node" do
99
99
  expect(@child_1_1.ancestors).to eq([@root, @child_1])
100
100
  end
101
101
  end
102
102
 
103
- describe 'self_and_ancestors' do
103
+ describe '#self_and_ancestors' do
104
104
  it "should return current node's ancestors node include self" do
105
105
  expect(@child_1_1.self_and_ancestors).to eq([@root, @child_1, @child_1_1])
106
106
  end
107
107
  end
108
108
 
109
- describe 'children' do
109
+ describe '#children' do
110
110
  it "should return current node's children node" do
111
111
  expect(@child_1.children).to eq([@child_1_1, @child_1_2, @child_1_3])
112
112
  end
113
113
  end
114
114
 
115
- describe 'descendants' do
115
+ describe '#descendants' do
116
116
  it "should return current node's all descendants" do
117
117
  expect(@child_1.descendants).to eq([@child_1_1, @child_1_2, @child_1_3, @child_1_2_1, @child_1_1_1])
118
118
  expect(@root.descendants).to eq([@child_1, @child_2, @child_1_1, @child_1_2, @child_1_3, @child_1_2_1, @child_1_1_1])
119
119
  end
120
120
  end
121
121
 
122
- describe 'self_and_descendants' do
122
+ describe '#self_and_descendants' do
123
123
  it "should return current node and it's all descendants" do
124
124
  expect(@child_1.self_and_descendants).to eq([@child_1, @child_1_1, @child_1_2, @child_1_3, @child_1_2_1, @child_1_1_1])
125
125
  expect(@root.self_and_descendants).to eq([@root, @child_1, @child_2, @child_1_1, @child_1_2, @child_1_3, @child_1_2_1, @child_1_1_1])
126
126
  end
127
127
  end
128
128
 
129
- describe 'right_sibling' do
129
+ describe '#right_sibling' do
130
130
  it "should return current node's right sibling" do
131
131
  expect(@child_1_2.right_sibling).to eq(@child_1_3)
132
132
  end
@@ -136,7 +136,7 @@ describe 'TextBasedNestedSet' do
136
136
  end
137
137
  end
138
138
 
139
- describe 'left_sibling' do
139
+ describe '#left_sibling' do
140
140
  it "should return current node's left sibling" do
141
141
  expect(@child_1_2.left_sibling).to eq(@child_1_1)
142
142
  end
@@ -146,7 +146,7 @@ describe 'TextBasedNestedSet' do
146
146
  end
147
147
  end
148
148
 
149
- describe 'root?' do
149
+ describe '#root?' do
150
150
  it "should return true if current node is root node" do
151
151
  expect(@root.root?).to eq(true)
152
152
  end
@@ -165,7 +165,7 @@ describe 'TextBasedNestedSet' do
165
165
  end
166
166
  end
167
167
 
168
- describe 'rebuild' do
168
+ describe '#rebuild' do
169
169
  it "should rebuild current node's all descendants, set path and position again " do
170
170
  @root.descendants.each do |d|
171
171
  d.update(path: nil, position: nil)
@@ -197,6 +197,30 @@ describe 'TextBasedNestedSet' do
197
197
  end
198
198
  end
199
199
 
200
+ describe 'position_valid?' do
201
+ it "should return true if all records' position in table is right" do
202
+ expect(Category.position_valid?).to eq(true)
203
+ end
204
+
205
+ it "should return false if some records' position in table is out of order" do
206
+ @child_2.position = 0
207
+ @child_2.save!
208
+ expect(Category.position_valid?).to eq(false)
209
+ end
210
+ end
211
+
212
+ describe '#check_position' do
213
+ it "should return true if current tree's children's position is right" do
214
+ expect(@child_1.check_position).to eq(true)
215
+ end
216
+
217
+ it "should return false if current tree's children's position is out of order" do
218
+ @child_1_3.position = 4
219
+ @child_1_3.save!
220
+ expect(@child_1.check_position).to eq(false)
221
+ end
222
+ end
223
+
200
224
  describe 'private destroy_descendants' do
201
225
  it 'should destroy descendants when itself be destroyed' do
202
226
  @child_1.destroy
@@ -206,7 +230,7 @@ describe 'TextBasedNestedSet' do
206
230
  end
207
231
 
208
232
  describe 'default value test' do
209
- it 'should create a root node' do
233
+ it 'should create a root node with default value: parent_id: 0, path: "/0/", position: 0' do
210
234
  root1 = create(:category, name: 'root1')
211
235
  expect(root1.parent_id).to eq(0)
212
236
  expect(root1.path).to eq('/0/')
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
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - beyondalbert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-12 00:00:00.000000000 Z
11
+ date: 2015-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler