acts-as-tree-with-dotted-ids 1.0.0 → 1.1.0

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.
@@ -172,7 +172,11 @@ module ActiveRecord
172
172
 
173
173
  # Returns the depth of the node, root nodes have a depth of 0
174
174
  def depth
175
- self.dotted_ids.scan(/\./).size
175
+ if self.dotted_ids.present?
176
+ self.dotted_ids.scan(/\./).size
177
+ else
178
+ (self.parent.try(:depth) || -1) + 1
179
+ end
176
180
  end
177
181
 
178
182
  protected
metadata CHANGED
@@ -1,88 +1,117 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: acts-as-tree-with-dotted-ids
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 0
9
- - 0
10
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - David Heinemeier Hansson
14
9
  - Xavier Defrang
15
10
  autorequire:
16
11
  bindir: bin
17
12
  cert_chain: []
18
-
19
- date: 2010-11-09 00:00:00 +01:00
20
- default_executable:
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
13
+ date: 2013-02-11 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jeweler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: sqlite3
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
23
48
  name: activerecord
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
24
56
  prerelease: false
25
- requirement: &id001 !ruby/object:Gem::Requirement
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: activerecord
65
+ requirement: !ruby/object:Gem::Requirement
26
66
  none: false
27
- requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- hash: 7
31
- segments:
32
- - 3
33
- - 0
34
- - 0
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
35
70
  version: 3.0.0
36
71
  type: :runtime
37
- version_requirements: *id001
38
- description: ""
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: 3.0.0
79
+ description: ''
39
80
  email: tma@freshbit.ch
40
81
  executables: []
41
-
42
82
  extensions: []
43
-
44
- extra_rdoc_files:
83
+ extra_rdoc_files:
45
84
  - README.rdoc
46
- files:
85
+ files:
47
86
  - lib/active_record/acts/tree_with_dotted_ids.rb
48
87
  - lib/acts-as-tree-with-dotted-ids.rb
49
88
  - README.rdoc
50
- - test/acts_as_tree_test.rb
51
- - test/schema.rb
52
- has_rdoc: true
53
89
  homepage: http://github.com/tma/acts-as-tree-with-dotted-ids
54
90
  licenses: []
55
-
56
91
  post_install_message:
57
- rdoc_options:
58
- - --charset=UTF-8
59
- require_paths:
92
+ rdoc_options: []
93
+ require_paths:
60
94
  - lib
61
- required_ruby_version: !ruby/object:Gem::Requirement
95
+ required_ruby_version: !ruby/object:Gem::Requirement
62
96
  none: false
63
- requirements:
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- hash: 3
67
- segments:
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
68
102
  - 0
69
- version: "0"
70
- required_rubygems_version: !ruby/object:Gem::Requirement
103
+ hash: 3584319109094579094
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
105
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- hash: 3
76
- segments:
77
- - 0
78
- version: "0"
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
79
110
  requirements: []
80
-
81
111
  rubyforge_project:
82
- rubygems_version: 1.3.7
112
+ rubygems_version: 1.8.24
83
113
  signing_key:
84
114
  specification_version: 3
85
- summary: A drop in replacement for acts_as_tree with super fast ancestors and subtree access
86
- test_files:
87
- - test/acts_as_tree_test.rb
88
- - test/schema.rb
115
+ summary: A drop in replacement for acts_as_tree with super fast ancestors and subtree
116
+ access
117
+ test_files: []
@@ -1,377 +0,0 @@
1
- require 'test/unit'
2
-
3
- require 'rubygems'
4
- require 'active_record'
5
-
6
- $:.unshift File.dirname(__FILE__) + '/../lib'
7
-
8
- require 'active_record/acts/tree_with_dotted_ids'
9
-
10
- require File.dirname(__FILE__) + '/../init'
11
-
12
- class Test::Unit::TestCase
13
- def assert_queries(num = 1)
14
- $query_count = 0
15
- yield
16
- ensure
17
- assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
18
- end
19
-
20
- def assert_no_queries(&block)
21
- assert_queries(0, &block)
22
- end
23
- end
24
-
25
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
26
-
27
- # AR keeps printing annoying schema statements
28
- $stdout = StringIO.new
29
-
30
- def setup_db
31
- ActiveRecord::Base.logger
32
- ActiveRecord::Schema.define(:version => 1) do
33
- create_table :mixins do |t|
34
- t.column :type, :string
35
- t.column :parent_id, :integer
36
- t.column :dotted_ids, :string
37
- t.column :name, :string
38
- end
39
- end
40
- end
41
-
42
- def teardown_db
43
- ActiveRecord::Base.connection.tables.each do |table|
44
- ActiveRecord::Base.connection.drop_table(table)
45
- end
46
- end
47
-
48
- class Mixin < ActiveRecord::Base
49
- end
50
-
51
- class TreeMixin < Mixin
52
- acts_as_tree_with_dotted_ids :foreign_key => "parent_id", :order => "id"
53
- end
54
-
55
- class TreeMixinWithoutOrder < Mixin
56
- acts_as_tree_with_dotted_ids :foreign_key => "parent_id"
57
- end
58
-
59
- class RecursivelyCascadedTreeMixin < Mixin
60
- acts_as_tree_with_dotted_ids :foreign_key => "parent_id"
61
- has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
62
- end
63
-
64
- class TreeTest < Test::Unit::TestCase
65
-
66
- def setup
67
- setup_db
68
- @root1 = TreeMixin.create!
69
- @root_child1 = TreeMixin.create! :parent_id => @root1.id
70
- @child1_child = TreeMixin.create! :parent_id => @root_child1.id
71
- @root_child2 = TreeMixin.create! :parent_id => @root1.id
72
- @root2 = TreeMixin.create!
73
- @root3 = TreeMixin.create!
74
- end
75
-
76
- def teardown
77
- teardown_db
78
- end
79
-
80
- def test_children
81
- assert_equal @root1.children, [@root_child1, @root_child2]
82
- assert_equal @root_child1.children, [@child1_child]
83
- assert_equal @child1_child.children, []
84
- assert_equal @root_child2.children, []
85
- end
86
-
87
- def test_parent
88
- assert_equal @root_child1.parent, @root1
89
- assert_equal @root_child1.parent, @root_child2.parent
90
- assert_nil @root1.parent
91
- end
92
-
93
- def test_delete
94
- assert_equal 6, TreeMixin.count
95
- @root1.destroy
96
- assert_equal 2, TreeMixin.count
97
- @root2.destroy
98
- @root3.destroy
99
- assert_equal 0, TreeMixin.count
100
- end
101
-
102
- def test_insert
103
- @extra = @root1.children.create
104
-
105
- assert @extra
106
-
107
- assert_equal @extra.parent, @root1
108
-
109
- assert_equal 3, @root1.children.size
110
- assert @root1.children.include?(@extra)
111
- assert @root1.children.include?(@root_child1)
112
- assert @root1.children.include?(@root_child2)
113
- end
114
-
115
- def test_ancestors
116
- assert_equal [], @root1.ancestors
117
- assert_equal [@root1], @root_child1.ancestors
118
- assert_equal [@root_child1, @root1], @child1_child.ancestors
119
- assert_equal [@root1], @root_child2.ancestors
120
- assert_equal [], @root2.ancestors
121
- assert_equal [], @root3.ancestors
122
- end
123
-
124
- def test_root
125
- assert_equal @root1, TreeMixin.root
126
- assert_equal @root1, @root1.root
127
- assert_equal @root1, @root_child1.root
128
- assert_equal @root1, @child1_child.root
129
- assert_equal @root1, @root_child2.root
130
- assert_equal @root2, @root2.root
131
- assert_equal @root3, @root3.root
132
- end
133
-
134
- def test_roots
135
- assert_equal [@root1, @root2, @root3], TreeMixin.roots
136
- end
137
-
138
- def test_siblings
139
- assert_equal [@root2, @root3], @root1.siblings
140
- assert_equal [@root_child2], @root_child1.siblings
141
- assert_equal [], @child1_child.siblings
142
- assert_equal [@root_child1], @root_child2.siblings
143
- assert_equal [@root1, @root3], @root2.siblings
144
- assert_equal [@root1, @root2], @root3.siblings
145
- end
146
-
147
- def test_self_and_siblings
148
- assert_equal [@root1, @root2, @root3], @root1.self_and_siblings
149
- assert_equal [@root_child1, @root_child2], @root_child1.self_and_siblings
150
- assert_equal [@child1_child], @child1_child.self_and_siblings
151
- assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
152
- assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
153
- assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
154
- end
155
- end
156
-
157
- class TreeTestWithEagerLoading < Test::Unit::TestCase
158
-
159
- def setup
160
- teardown_db
161
- setup_db
162
- @root1 = TreeMixin.create!
163
- @root_child1 = TreeMixin.create! :parent_id => @root1.id
164
- @child1_child = TreeMixin.create! :parent_id => @root_child1.id
165
- @root_child2 = TreeMixin.create! :parent_id => @root1.id
166
- @root2 = TreeMixin.create!
167
- @root3 = TreeMixin.create!
168
-
169
- @rc1 = RecursivelyCascadedTreeMixin.create!
170
- @rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
171
- @rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
172
- @rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
173
- end
174
-
175
- def teardown
176
- teardown_db
177
- end
178
-
179
- def test_eager_association_loading
180
- roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
181
- assert_equal [@root1, @root2, @root3], roots
182
- assert_no_queries do
183
- assert_equal 2, roots[0].children.size
184
- assert_equal 0, roots[1].children.size
185
- assert_equal 0, roots[2].children.size
186
- end
187
- end
188
-
189
- def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
190
- root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
191
- assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
192
- end
193
-
194
- def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
195
- root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
196
- assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
197
- end
198
-
199
- def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
200
- leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
201
- assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
202
- end
203
- end
204
-
205
- class TreeTestWithoutOrder < Test::Unit::TestCase
206
-
207
- def setup
208
- setup_db
209
- @root1 = TreeMixinWithoutOrder.create!
210
- @root2 = TreeMixinWithoutOrder.create!
211
- end
212
-
213
- def teardown
214
- teardown_db
215
- end
216
-
217
- def test_root
218
- assert [@root1, @root2].include?(TreeMixinWithoutOrder.root)
219
- end
220
-
221
- def test_roots
222
- assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots
223
- end
224
- end
225
-
226
- class TestDottedIdTree < Test::Unit::TestCase
227
-
228
- def setup
229
- setup_db
230
- @tree = TreeMixin.create(:name => 'Root')
231
- @child = @tree.children.create(:name => 'Child')
232
- @subchild = @child.children.create(:name => 'Subchild')
233
- @new_root = TreeMixin.create!(:name => 'New Root')
234
- end
235
-
236
- def teardown
237
- teardown_db
238
- end
239
-
240
- def test_build_dotted_ids
241
- assert_equal "#{@tree.id}", @tree.dotted_ids
242
- assert_equal "#{@tree.id}.#{@child.id}", @child.dotted_ids
243
- assert_equal "#{@tree.id}.#{@child.id}.#{@subchild.id}", @subchild.dotted_ids
244
- end
245
-
246
- def test_ancestor_of
247
-
248
- assert @tree.ancestor_of?(@child)
249
- assert @child.ancestor_of?(@subchild)
250
- assert @tree.ancestor_of?(@subchild)
251
-
252
- assert !@tree.ancestor_of?(@tree)
253
- assert !@child.ancestor_of?(@child)
254
- assert !@subchild.ancestor_of?(@subchild)
255
-
256
- assert !@child.ancestor_of?(@tree)
257
- assert !@subchild.ancestor_of?(@tree)
258
- assert !@subchild.ancestor_of?(@child)
259
-
260
- end
261
-
262
- def test_descendant_of
263
-
264
- assert @child.descendant_of?(@tree)
265
- assert @subchild.descendant_of?(@child)
266
- assert @subchild.descendant_of?(@tree)
267
-
268
- assert !@tree.descendant_of?(@tree)
269
- assert !@child.descendant_of?(@child)
270
- assert !@subchild.descendant_of?(@subchild)
271
-
272
- assert !@tree.descendant_of?(@child)
273
- assert !@child.descendant_of?(@subchild)
274
- assert !@tree.descendant_of?(@subchild)
275
-
276
- end
277
-
278
-
279
- def test_all_children
280
-
281
- kids = @tree.all_children
282
- assert_kind_of Array, kids
283
- assert kids.size == 2
284
- assert !kids.include?(@tree)
285
- assert kids.include?(@child)
286
- assert kids.include?(@subchild)
287
-
288
- kids = @child.all_children
289
- assert_kind_of Array, kids
290
- assert kids.size == 1
291
- assert !kids.include?(@child)
292
- assert kids.include?(@subchild)
293
-
294
- kids = @subchild.all_children
295
- assert_kind_of Array, kids
296
- assert kids.empty?
297
-
298
- end
299
-
300
- def test_rebuild
301
-
302
- @tree.parent_id = @new_root.id
303
- @tree.save
304
-
305
- @new_root.reload
306
- @root = @new_root.children.first
307
- @child = @root.children.first
308
- @subchild = @child.children.first
309
-
310
- assert_equal "#{@new_root.id}", @new_root.dotted_ids
311
- assert_equal "#{@new_root.id}.#{@tree.id}", @tree.dotted_ids
312
- assert_equal "#{@new_root.id}.#{@tree.id}.#{@child.id}", @child.dotted_ids
313
- assert_equal "#{@new_root.id}.#{@tree.id}.#{@child.id}.#{@subchild.id}", @subchild.dotted_ids
314
- assert @tree.ancestor_of?(@subchild)
315
- assert @new_root.ancestor_of?(@tree)
316
-
317
- @subchild.parent = @tree
318
- @subchild.save
319
-
320
- assert_equal "#{@new_root.id}", @new_root.dotted_ids
321
- assert_equal "#{@new_root.id}.#{@tree.id}", @tree.dotted_ids
322
- assert_equal "#{@new_root.id}.#{@tree.id}.#{@child.id}", @child.dotted_ids
323
- assert_equal "#{@new_root.id}.#{@tree.id}.#{@subchild.id}", @subchild.dotted_ids
324
-
325
- @child.parent = nil
326
- @child.save!
327
-
328
- assert_equal "#{@new_root.id}", @new_root.dotted_ids
329
- assert_equal "#{@new_root.id}.#{@tree.id}", @tree.dotted_ids
330
- assert_equal "#{@child.id}", @child.dotted_ids
331
- assert_equal "#{@new_root.id}.#{@tree.id}.#{@subchild.id}", @subchild.dotted_ids
332
-
333
- end
334
-
335
- def test_ancestors
336
- assert @tree.ancestors.empty?
337
- assert_equal [@tree], @child.ancestors
338
- assert_equal [@child, @tree], @subchild.ancestors
339
- end
340
-
341
- def test_root
342
- assert_equal @tree, @tree.root
343
- assert_equal @tree, @child.root
344
- assert_equal @tree, @subchild.root
345
- end
346
-
347
- def test_traverse
348
-
349
- traversed_nodes = []
350
- TreeMixin.traverse { |node| traversed_nodes << node }
351
-
352
- assert_equal [@tree, @child, @subchild, @new_root], traversed_nodes
353
-
354
- end
355
-
356
- def test_rebuild_dotted_ids
357
-
358
- TreeMixin.update_all('dotted_ids = NULL')
359
- assert TreeMixin.find(:all).all? { |n| n.dotted_ids.blank? }
360
- @subchild.reload
361
- assert_nil @subchild.dotted_ids
362
-
363
- TreeMixin.rebuild_dotted_ids!
364
- assert TreeMixin.find(:all).all? { |n| n.dotted_ids.present? }
365
- @subchild.reload
366
- assert_equal "#{@tree.id}.#{@child.id}.#{@subchild.id}", @subchild.dotted_ids
367
-
368
- end
369
-
370
- def test_depth
371
- assert_equal 0, @tree.depth
372
- assert_equal 1, @child.depth
373
- assert_equal 2, @subchild.depth
374
- end
375
-
376
- end
377
-
data/test/schema.rb DELETED
File without changes