awesome_nested_set 1.4.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,9 +32,9 @@ module CollectiveIdea #:nodoc:
32
32
  end.compact
33
33
  end
34
34
  result
35
- end
36
-
35
+ end
36
+
37
37
  end
38
- end
38
+ end
39
39
  end
40
40
  end
@@ -0,0 +1,3 @@
1
+ module AwesomeNestedSet
2
+ VERSION = '2.0.0' unless defined?(::AwesomeNestedSet::VERSION)
3
+ end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awesome_nested_set
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
4
  prerelease:
6
- segments:
7
- - 1
8
- - 4
9
- - 4
10
- version: 1.4.4
5
+ version: 2.0.0
11
6
  platform: ruby
12
7
  authors:
13
8
  - Brandon Keepers
@@ -27,11 +22,7 @@ dependencies:
27
22
  requirements:
28
23
  - - ">="
29
24
  - !ruby/object:Gem::Version
30
- hash: 13
31
- segments:
32
- - 1
33
- - 1
34
- version: "1.1"
25
+ version: 3.0.0
35
26
  type: :runtime
36
27
  version_requirements: *id001
37
28
  description: An awesome nested set implementation for Active Record
@@ -43,26 +34,13 @@ extensions: []
43
34
  extra_rdoc_files:
44
35
  - README.rdoc
45
36
  files:
46
- - .autotest
37
+ - lib/awesome_nested_set/awesome_nested_set.rb
38
+ - lib/awesome_nested_set/helper.rb
39
+ - lib/awesome_nested_set/version.rb
40
+ - lib/awesome_nested_set.rb
47
41
  - MIT-LICENSE
48
42
  - README.rdoc
49
- - Rakefile
50
- - VERSION
51
- - awesome_nested_set.gemspec
52
- - init.rb
53
- - lib/awesome_nested_set.rb
54
- - lib/awesome_nested_set/helper.rb
55
- - rails/init.rb
56
- - test/application.rb
57
- - test/awesome_nested_set/helper_test.rb
58
- - test/awesome_nested_set_test.rb
59
- - test/db/database.yml
60
- - test/db/schema.rb
61
- - test/fixtures/categories.yml
62
- - test/fixtures/category.rb
63
- - test/fixtures/departments.yml
64
- - test/fixtures/notes.yml
65
- - test/test_helper.rb
43
+ - CHANGELOG
66
44
  has_rdoc: true
67
45
  homepage: http://github.com/collectiveidea/awesome_nested_set
68
46
  licenses: []
@@ -80,18 +58,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
58
  requirements:
81
59
  - - ">="
82
60
  - !ruby/object:Gem::Version
83
- hash: 3
84
- segments:
85
- - 0
86
61
  version: "0"
87
62
  required_rubygems_version: !ruby/object:Gem::Requirement
88
63
  none: false
89
64
  requirements:
90
65
  - - ">="
91
66
  - !ruby/object:Gem::Version
92
- hash: 3
93
- segments:
94
- - 0
95
67
  version: "0"
96
68
  requirements: []
97
69
 
@@ -100,14 +72,5 @@ rubygems_version: 1.5.2
100
72
  signing_key:
101
73
  specification_version: 3
102
74
  summary: An awesome nested set implementation for Active Record
103
- test_files:
104
- - test/db/database.yml
105
- - test/fixtures/categories.yml
106
- - test/fixtures/departments.yml
107
- - test/fixtures/notes.yml
108
- - test/application.rb
109
- - test/awesome_nested_set/helper_test.rb
110
- - test/awesome_nested_set_test.rb
111
- - test/db/schema.rb
112
- - test/fixtures/category.rb
113
- - test/test_helper.rb
75
+ test_files: []
76
+
data/.autotest DELETED
@@ -1,13 +0,0 @@
1
- Autotest.add_hook :initialize do |at|
2
- at.clear_mappings
3
-
4
- at.add_mapping %r%^lib/(.*)\.rb$% do |_, m|
5
- at.files_matching %r%^test/#{m[1]}_test.rb$%
6
- end
7
-
8
- at.add_mapping(%r%^test/.*\.rb$%) {|filename, _| filename }
9
-
10
- at.add_mapping %r%^test/fixtures/(.*)s.yml% do |_, _|
11
- at.files_matching %r%^test/.*\.rb$%
12
- end
13
- end
data/Rakefile DELETED
@@ -1,56 +0,0 @@
1
- require 'rubygems'
2
- begin
3
- require 'jeweler'
4
- rescue LoadError
5
- puts "Jeweler not available. Install it with: sudo gem install jeweler"
6
- exit 1
7
- end
8
- require 'rake/testtask'
9
- require 'rake/rdoctask'
10
- require 'rcov/rcovtask'
11
- require "load_multi_rails_rake_tasks"
12
-
13
- Jeweler::Tasks.new do |s|
14
- s.name = "awesome_nested_set"
15
- s.summary = "An awesome nested set implementation for Active Record"
16
- s.description = s.summary
17
- s.email = "info@collectiveidea.com"
18
- s.homepage = "http://github.com/collectiveidea/awesome_nested_set"
19
- s.authors = ["Brandon Keepers", "Daniel Morrison"]
20
- s.add_dependency "activerecord", ['>= 1.1']
21
- s.has_rdoc = true
22
- s.extra_rdoc_files = [ "README.rdoc"]
23
- s.rdoc_options = ["--main", "README.rdoc", "--inline-source", "--line-numbers"]
24
- s.test_files = Dir['test/**/*.{yml,rb}']
25
- end
26
- Jeweler::GemcutterTasks.new
27
-
28
- desc 'Default: run unit tests.'
29
- task :default => :test
30
-
31
- desc 'Test the awesome_nested_set plugin.'
32
- Rake::TestTask.new(:test) do |t|
33
- t.libs += ['lib', 'test']
34
- t.pattern = 'test/**/*_test.rb'
35
- t.verbose = true
36
- end
37
-
38
- desc 'Generate documentation for the awesome_nested_set plugin.'
39
- Rake::RDocTask.new(:rdoc) do |rdoc|
40
- rdoc.rdoc_dir = 'rdoc'
41
- rdoc.title = 'AwesomeNestedSet'
42
- rdoc.options << '--line-numbers' << '--inline-source'
43
- rdoc.rdoc_files.include('README.rdoc')
44
- rdoc.rdoc_files.include('lib/**/*.rb')
45
- end
46
-
47
- namespace :test do
48
- desc "just rcov minus html output"
49
- Rcov::RcovTask.new(:coverage) do |t|
50
- t.libs << 'test'
51
- t.test_files = FileList['test/**/*_test.rb']
52
- t.output_dir = 'coverage'
53
- t.verbose = true
54
- t.rcov_opts = %w(--exclude test,/usr/lib/ruby,/Library/Ruby,lib/awesome_nested_set/named_scope.rb --sort coverage)
55
- end
56
- end
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.4.4
@@ -1,59 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{awesome_nested_set}
8
- s.version = "1.4.4"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Brandon Keepers", "Daniel Morrison"]
12
- s.date = %q{2011-05-10}
13
- s.description = %q{An awesome nested set implementation for Active Record}
14
- s.email = %q{info@collectiveidea.com}
15
- s.extra_rdoc_files = [
16
- "README.rdoc"
17
- ]
18
- s.files = [
19
- ".autotest",
20
- "MIT-LICENSE",
21
- "README.rdoc",
22
- "Rakefile",
23
- "VERSION",
24
- "awesome_nested_set.gemspec",
25
- "init.rb",
26
- "lib/awesome_nested_set.rb",
27
- "lib/awesome_nested_set/helper.rb",
28
- "rails/init.rb",
29
- "test/application.rb",
30
- "test/awesome_nested_set/helper_test.rb",
31
- "test/awesome_nested_set_test.rb",
32
- "test/db/database.yml",
33
- "test/db/schema.rb",
34
- "test/fixtures/categories.yml",
35
- "test/fixtures/category.rb",
36
- "test/fixtures/departments.yml",
37
- "test/fixtures/notes.yml",
38
- "test/test_helper.rb"
39
- ]
40
- s.homepage = %q{http://github.com/collectiveidea/awesome_nested_set}
41
- s.rdoc_options = ["--main", "README.rdoc", "--inline-source", "--line-numbers"]
42
- s.require_paths = ["lib"]
43
- s.rubygems_version = %q{1.5.2}
44
- s.summary = %q{An awesome nested set implementation for Active Record}
45
- s.test_files = ["test/db/database.yml", "test/fixtures/categories.yml", "test/fixtures/departments.yml", "test/fixtures/notes.yml", "test/application.rb", "test/awesome_nested_set/helper_test.rb", "test/awesome_nested_set_test.rb", "test/db/schema.rb", "test/fixtures/category.rb", "test/test_helper.rb"]
46
-
47
- if s.respond_to? :specification_version then
48
- s.specification_version = 3
49
-
50
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
- s.add_runtime_dependency(%q<activerecord>, [">= 1.1"])
52
- else
53
- s.add_dependency(%q<activerecord>, [">= 1.1"])
54
- end
55
- else
56
- s.add_dependency(%q<activerecord>, [">= 1.1"])
57
- end
58
- end
59
-
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require File.dirname(__FILE__) + "/rails/init"
@@ -1,12 +0,0 @@
1
- require 'awesome_nested_set'
2
-
3
- ActiveRecord::Base.class_eval do
4
- include CollectiveIdea::Acts::NestedSet
5
- end
6
-
7
- if defined?(ActionView)
8
- require 'awesome_nested_set/helper'
9
- ActionView::Base.class_eval do
10
- include CollectiveIdea::Acts::NestedSet::Helper
11
- end
12
- end
@@ -1 +0,0 @@
1
- # This file is here to satisfy test_help from Rails < 2.3
@@ -1,41 +0,0 @@
1
- require 'test_helper'
2
-
3
- module CollectiveIdea
4
- module Acts #:nodoc:
5
- module NestedSet #:nodoc:
6
- class AwesomeNestedSetTest < TestCaseClass
7
- include Helper
8
- fixtures :categories
9
-
10
- def test_nested_set_options
11
- expected = [
12
- [" Top Level", 1],
13
- ["- Child 1", 2],
14
- ['- Child 2', 3],
15
- ['-- Child 2.1', 4],
16
- ['- Child 3', 5],
17
- [" Top Level 2", 6]
18
- ]
19
- actual = nested_set_options(Category) do |c|
20
- "#{'-' * c.level} #{c.name}"
21
- end
22
- assert_equal expected, actual
23
- end
24
-
25
- def test_nested_set_options_with_mover
26
- expected = [
27
- [" Top Level", 1],
28
- ["- Child 1", 2],
29
- ['- Child 3', 5],
30
- [" Top Level 2", 6]
31
- ]
32
- actual = nested_set_options(Category, categories(:child_2)) do |c|
33
- "#{'-' * c.level} #{c.name}"
34
- end
35
- assert_equal expected, actual
36
- end
37
-
38
- end
39
- end
40
- end
41
- end
@@ -1,801 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Note < ActiveRecord::Base
4
- acts_as_nested_set :scope => [:notable_id, :notable_type]
5
- end
6
- class Default < ActiveRecord::Base
7
- acts_as_nested_set
8
- set_table_name 'categories'
9
- end
10
- class ScopedCategory < ActiveRecord::Base
11
- acts_as_nested_set :scope => :organization
12
- set_table_name 'categories'
13
- end
14
- class RenamedColumns < ActiveRecord::Base
15
- acts_as_nested_set :parent_column => 'mother_id', :left_column => 'red', :right_column => 'black'
16
- end
17
-
18
- class AwesomeNestedSetTest < TestCaseClass
19
-
20
- def test_left_column_default
21
- assert_equal 'lft', Default.acts_as_nested_set_options[:left_column]
22
- end
23
-
24
- def test_right_column_default
25
- assert_equal 'rgt', Default.acts_as_nested_set_options[:right_column]
26
- end
27
-
28
- def test_parent_column_default
29
- assert_equal 'parent_id', Default.acts_as_nested_set_options[:parent_column]
30
- end
31
-
32
- def test_scope_default
33
- assert_nil Default.acts_as_nested_set_options[:scope]
34
- end
35
-
36
- def test_left_column_name
37
- assert_equal 'lft', Default.left_column_name
38
- assert_equal 'lft', Default.new.left_column_name
39
- assert_equal 'red', RenamedColumns.left_column_name
40
- assert_equal 'red', RenamedColumns.new.left_column_name
41
- end
42
-
43
- def test_right_column_name
44
- assert_equal 'rgt', Default.right_column_name
45
- assert_equal 'rgt', Default.new.right_column_name
46
- assert_equal 'black', RenamedColumns.right_column_name
47
- assert_equal 'black', RenamedColumns.new.right_column_name
48
- end
49
-
50
- def test_parent_column_name
51
- assert_equal 'parent_id', Default.parent_column_name
52
- assert_equal 'parent_id', Default.new.parent_column_name
53
- assert_equal 'mother_id', RenamedColumns.parent_column_name
54
- assert_equal 'mother_id', RenamedColumns.new.parent_column_name
55
- end
56
-
57
- def test_creation_with_altered_column_names
58
- assert_nothing_raised do
59
- RenamedColumns.create!()
60
- end
61
- end
62
-
63
- def test_quoted_left_column_name
64
- quoted = Default.connection.quote_column_name('lft')
65
- assert_equal quoted, Default.quoted_left_column_name
66
- assert_equal quoted, Default.new.quoted_left_column_name
67
- end
68
-
69
- def test_quoted_right_column_name
70
- quoted = Default.connection.quote_column_name('rgt')
71
- assert_equal quoted, Default.quoted_right_column_name
72
- assert_equal quoted, Default.new.quoted_right_column_name
73
- end
74
-
75
- def test_left_column_protected_from_assignment
76
- assert_raises(ActiveRecord::ActiveRecordError) { Category.new.lft = 1 }
77
- end
78
-
79
- def test_right_column_protected_from_assignment
80
- assert_raises(ActiveRecord::ActiveRecordError) { Category.new.rgt = 1 }
81
- end
82
-
83
- def test_colums_protected_on_initialize
84
- c = Category.new(:lft => 1, :rgt => 2)
85
- assert_nil c.lft
86
- assert_nil c.rgt
87
- end
88
-
89
- def test_scoped_appends_id
90
- assert_equal :organization_id, ScopedCategory.acts_as_nested_set_options[:scope]
91
- end
92
-
93
- def test_roots_class_method
94
- assert_equal Category.find_all_by_parent_id(nil), Category.roots
95
- end
96
-
97
- def test_root_class_method
98
- assert_equal categories(:top_level), Category.root
99
- end
100
-
101
- def test_root
102
- assert_equal categories(:top_level), categories(:child_3).root
103
- end
104
-
105
- def test_root?
106
- assert categories(:top_level).root?
107
- assert categories(:top_level_2).root?
108
- end
109
-
110
- def test_leaves_class_method
111
- assert_equal Category.find(:all, :conditions => "#{Category.right_column_name} - #{Category.left_column_name} = 1"), Category.leaves
112
- assert_equal Category.leaves.count, 4
113
- assert (Category.leaves.include? categories(:child_1))
114
- assert (Category.leaves.include? categories(:child_2_1))
115
- assert (Category.leaves.include? categories(:child_3))
116
- assert (Category.leaves.include? categories(:top_level_2))
117
- end
118
-
119
- def test_leaf
120
- assert categories(:child_1).leaf?
121
- assert categories(:child_2_1).leaf?
122
- assert categories(:child_3).leaf?
123
- assert categories(:top_level_2).leaf?
124
-
125
- assert !categories(:top_level).leaf?
126
- assert !categories(:child_2).leaf?
127
- assert !Category.new.leaf?
128
- end
129
-
130
-
131
- def test_parent
132
- assert_equal categories(:child_2), categories(:child_2_1).parent
133
- end
134
-
135
- def test_self_and_ancestors
136
- child = categories(:child_2_1)
137
- self_and_ancestors = [categories(:top_level), categories(:child_2), child]
138
- assert_equal self_and_ancestors, child.self_and_ancestors
139
- end
140
-
141
- def test_ancestors
142
- child = categories(:child_2_1)
143
- ancestors = [categories(:top_level), categories(:child_2)]
144
- assert_equal ancestors, child.ancestors
145
- end
146
-
147
- def test_self_and_siblings
148
- child = categories(:child_2)
149
- self_and_siblings = [categories(:child_1), child, categories(:child_3)]
150
- assert_equal self_and_siblings, child.self_and_siblings
151
- assert_nothing_raised do
152
- tops = [categories(:top_level), categories(:top_level_2)]
153
- assert_equal tops, categories(:top_level).self_and_siblings
154
- end
155
- end
156
-
157
- def test_siblings
158
- child = categories(:child_2)
159
- siblings = [categories(:child_1), categories(:child_3)]
160
- assert_equal siblings, child.siblings
161
- end
162
-
163
- def test_leaves
164
- leaves = [categories(:child_1), categories(:child_2_1), categories(:child_3), categories(:top_level_2)]
165
- assert categories(:top_level).leaves, leaves
166
- end
167
-
168
- def test_level
169
- assert_equal 0, categories(:top_level).level
170
- assert_equal 1, categories(:child_1).level
171
- assert_equal 2, categories(:child_2_1).level
172
- end
173
-
174
- def test_has_children?
175
- assert categories(:child_2_1).children.empty?
176
- assert !categories(:child_2).children.empty?
177
- assert !categories(:top_level).children.empty?
178
- end
179
-
180
- def test_self_and_descendents
181
- parent = categories(:top_level)
182
- self_and_descendants = [parent, categories(:child_1), categories(:child_2),
183
- categories(:child_2_1), categories(:child_3)]
184
- assert_equal self_and_descendants, parent.self_and_descendants
185
- assert_equal self_and_descendants, parent.self_and_descendants.count
186
- end
187
-
188
- def test_descendents
189
- lawyers = Category.create!(:name => "lawyers")
190
- us = Category.create!(:name => "United States")
191
- us.move_to_child_of(lawyers)
192
- patent = Category.create!(:name => "Patent Law")
193
- patent.move_to_child_of(us)
194
- lawyers.reload
195
-
196
- assert_equal 1, lawyers.children.size
197
- assert_equal 1, us.children.size
198
- assert_equal 2, lawyers.descendants.size
199
- end
200
-
201
- def test_self_and_descendents
202
- parent = categories(:top_level)
203
- descendants = [categories(:child_1), categories(:child_2),
204
- categories(:child_2_1), categories(:child_3)]
205
- assert_equal descendants, parent.descendants
206
- end
207
-
208
- def test_children
209
- category = categories(:top_level)
210
- category.children.each {|c| assert_equal category.id, c.parent_id }
211
- end
212
-
213
- def test_order_of_children
214
- categories(:child_2).move_left
215
- assert_equal categories(:child_2), categories(:top_level).children[0]
216
- assert_equal categories(:child_1), categories(:top_level).children[1]
217
- assert_equal categories(:child_3), categories(:top_level).children[2]
218
- end
219
-
220
- def test_is_or_is_ancestor_of?
221
- assert categories(:top_level).is_or_is_ancestor_of?(categories(:child_1))
222
- assert categories(:top_level).is_or_is_ancestor_of?(categories(:child_2_1))
223
- assert categories(:child_2).is_or_is_ancestor_of?(categories(:child_2_1))
224
- assert !categories(:child_2_1).is_or_is_ancestor_of?(categories(:child_2))
225
- assert !categories(:child_1).is_or_is_ancestor_of?(categories(:child_2))
226
- assert categories(:child_1).is_or_is_ancestor_of?(categories(:child_1))
227
- end
228
-
229
- def test_is_ancestor_of?
230
- assert categories(:top_level).is_ancestor_of?(categories(:child_1))
231
- assert categories(:top_level).is_ancestor_of?(categories(:child_2_1))
232
- assert categories(:child_2).is_ancestor_of?(categories(:child_2_1))
233
- assert !categories(:child_2_1).is_ancestor_of?(categories(:child_2))
234
- assert !categories(:child_1).is_ancestor_of?(categories(:child_2))
235
- assert !categories(:child_1).is_ancestor_of?(categories(:child_1))
236
- end
237
-
238
- def test_is_or_is_ancestor_of_with_scope
239
- root = ScopedCategory.root
240
- child = root.children.first
241
- assert root.is_or_is_ancestor_of?(child)
242
- child.update_attribute :organization_id, 'different'
243
- assert !root.is_or_is_ancestor_of?(child)
244
- end
245
-
246
- def test_is_or_is_descendant_of?
247
- assert categories(:child_1).is_or_is_descendant_of?(categories(:top_level))
248
- assert categories(:child_2_1).is_or_is_descendant_of?(categories(:top_level))
249
- assert categories(:child_2_1).is_or_is_descendant_of?(categories(:child_2))
250
- assert !categories(:child_2).is_or_is_descendant_of?(categories(:child_2_1))
251
- assert !categories(:child_2).is_or_is_descendant_of?(categories(:child_1))
252
- assert categories(:child_1).is_or_is_descendant_of?(categories(:child_1))
253
- end
254
-
255
- def test_is_descendant_of?
256
- assert categories(:child_1).is_descendant_of?(categories(:top_level))
257
- assert categories(:child_2_1).is_descendant_of?(categories(:top_level))
258
- assert categories(:child_2_1).is_descendant_of?(categories(:child_2))
259
- assert !categories(:child_2).is_descendant_of?(categories(:child_2_1))
260
- assert !categories(:child_2).is_descendant_of?(categories(:child_1))
261
- assert !categories(:child_1).is_descendant_of?(categories(:child_1))
262
- end
263
-
264
- def test_is_or_is_descendant_of_with_scope
265
- root = ScopedCategory.root
266
- child = root.children.first
267
- assert child.is_or_is_descendant_of?(root)
268
- child.update_attribute :organization_id, 'different'
269
- assert !child.is_or_is_descendant_of?(root)
270
- end
271
-
272
- def test_same_scope?
273
- root = ScopedCategory.root
274
- child = root.children.first
275
- assert child.same_scope?(root)
276
- child.update_attribute :organization_id, 'different'
277
- assert !child.same_scope?(root)
278
- end
279
-
280
- def test_left_sibling
281
- assert_equal categories(:child_1), categories(:child_2).left_sibling
282
- assert_equal categories(:child_2), categories(:child_3).left_sibling
283
- end
284
-
285
- def test_left_sibling_of_root
286
- assert_nil categories(:top_level).left_sibling
287
- end
288
-
289
- def test_left_sibling_without_siblings
290
- assert_nil categories(:child_2_1).left_sibling
291
- end
292
-
293
- def test_left_sibling_of_leftmost_node
294
- assert_nil categories(:child_1).left_sibling
295
- end
296
-
297
- def test_right_sibling
298
- assert_equal categories(:child_3), categories(:child_2).right_sibling
299
- assert_equal categories(:child_2), categories(:child_1).right_sibling
300
- end
301
-
302
- def test_right_sibling_of_root
303
- assert_equal categories(:top_level_2), categories(:top_level).right_sibling
304
- assert_nil categories(:top_level_2).right_sibling
305
- end
306
-
307
- def test_right_sibling_without_siblings
308
- assert_nil categories(:child_2_1).right_sibling
309
- end
310
-
311
- def test_right_sibling_of_rightmost_node
312
- assert_nil categories(:child_3).right_sibling
313
- end
314
-
315
- def test_move_left
316
- categories(:child_2).move_left
317
- assert_nil categories(:child_2).left_sibling
318
- assert_equal categories(:child_1), categories(:child_2).right_sibling
319
- assert Category.valid?
320
- end
321
-
322
- def test_move_right
323
- categories(:child_2).move_right
324
- assert_nil categories(:child_2).right_sibling
325
- assert_equal categories(:child_3), categories(:child_2).left_sibling
326
- assert Category.valid?
327
- end
328
-
329
- def test_move_to_left_of
330
- categories(:child_3).move_to_left_of(categories(:child_1))
331
- assert_nil categories(:child_3).left_sibling
332
- assert_equal categories(:child_1), categories(:child_3).right_sibling
333
- assert Category.valid?
334
- end
335
-
336
- def test_move_to_right_of
337
- categories(:child_1).move_to_right_of(categories(:child_3))
338
- assert_nil categories(:child_1).right_sibling
339
- assert_equal categories(:child_3), categories(:child_1).left_sibling
340
- assert Category.valid?
341
- end
342
-
343
- def test_move_to_root
344
- categories(:child_2).move_to_root
345
- assert_nil categories(:child_2).parent
346
- assert_equal 0, categories(:child_2).level
347
- assert_equal 1, categories(:child_2_1).level
348
- assert_equal 1, categories(:child_2).left
349
- assert_equal 4, categories(:child_2).right
350
- assert Category.valid?
351
- end
352
-
353
- def test_move_to_child_of
354
- categories(:child_1).move_to_child_of(categories(:child_3))
355
- assert_equal categories(:child_3).id, categories(:child_1).parent_id
356
- assert Category.valid?
357
- end
358
-
359
- def test_move_to_child_of_appends_to_end
360
- child = Category.create! :name => 'New Child'
361
- child.move_to_child_of categories(:top_level)
362
- assert_equal child, categories(:top_level).children.last
363
- end
364
-
365
- def test_subtree_move_to_child_of
366
- assert_equal 4, categories(:child_2).left
367
- assert_equal 7, categories(:child_2).right
368
-
369
- assert_equal 2, categories(:child_1).left
370
- assert_equal 3, categories(:child_1).right
371
-
372
- categories(:child_2).move_to_child_of(categories(:child_1))
373
- assert Category.valid?
374
- assert_equal categories(:child_1).id, categories(:child_2).parent_id
375
-
376
- assert_equal 3, categories(:child_2).left
377
- assert_equal 6, categories(:child_2).right
378
- assert_equal 2, categories(:child_1).left
379
- assert_equal 7, categories(:child_1).right
380
- end
381
-
382
- def test_slightly_difficult_move_to_child_of
383
- assert_equal 11, categories(:top_level_2).left
384
- assert_equal 12, categories(:top_level_2).right
385
-
386
- # create a new top-level node and move single-node top-level tree inside it.
387
- new_top = Category.create(:name => 'New Top')
388
- assert_equal 13, new_top.left
389
- assert_equal 14, new_top.right
390
-
391
- categories(:top_level_2).move_to_child_of(new_top)
392
-
393
- assert Category.valid?
394
- assert_equal new_top.id, categories(:top_level_2).parent_id
395
-
396
- assert_equal 12, categories(:top_level_2).left
397
- assert_equal 13, categories(:top_level_2).right
398
- assert_equal 11, new_top.left
399
- assert_equal 14, new_top.right
400
- end
401
-
402
- def test_difficult_move_to_child_of
403
- assert_equal 1, categories(:top_level).left
404
- assert_equal 10, categories(:top_level).right
405
- assert_equal 5, categories(:child_2_1).left
406
- assert_equal 6, categories(:child_2_1).right
407
-
408
- # create a new top-level node and move an entire top-level tree inside it.
409
- new_top = Category.create(:name => 'New Top')
410
- categories(:top_level).move_to_child_of(new_top)
411
- categories(:child_2_1).reload
412
- assert Category.valid?
413
- assert_equal new_top.id, categories(:top_level).parent_id
414
-
415
- assert_equal 4, categories(:top_level).left
416
- assert_equal 13, categories(:top_level).right
417
- assert_equal 8, categories(:child_2_1).left
418
- assert_equal 9, categories(:child_2_1).right
419
- end
420
-
421
- #rebuild swaps the position of the 2 children when added using move_to_child twice onto same parent
422
- def test_move_to_child_more_than_once_per_parent_rebuild
423
- root1 = Category.create(:name => 'Root1')
424
- root2 = Category.create(:name => 'Root2')
425
- root3 = Category.create(:name => 'Root3')
426
-
427
- root2.move_to_child_of root1
428
- root3.move_to_child_of root1
429
-
430
- output = Category.roots.last.to_text
431
- Category.update_all('lft = null, rgt = null')
432
- Category.rebuild!
433
-
434
- assert_equal Category.roots.last.to_text, output
435
- end
436
-
437
- # doing move_to_child twice onto same parent from the furthest right first
438
- def test_move_to_child_more_than_once_per_parent_outside_in
439
- node1 = Category.create(:name => 'Node-1')
440
- node2 = Category.create(:name => 'Node-2')
441
- node3 = Category.create(:name => 'Node-3')
442
-
443
- node2.move_to_child_of node1
444
- node3.move_to_child_of node1
445
-
446
- output = Category.roots.last.to_text
447
- Category.update_all('lft = null, rgt = null')
448
- Category.rebuild!
449
-
450
- assert_equal Category.roots.last.to_text, output
451
- end
452
-
453
-
454
- def test_valid_with_null_lefts
455
- assert Category.valid?
456
- Category.update_all('lft = null')
457
- assert !Category.valid?
458
- end
459
-
460
- def test_valid_with_null_rights
461
- assert Category.valid?
462
- Category.update_all('rgt = null')
463
- assert !Category.valid?
464
- end
465
-
466
- def test_valid_with_missing_intermediate_node
467
- # Even though child_2_1 will still exist, it is a sign of a sloppy delete, not an invalid tree.
468
- assert Category.valid?
469
- Category.delete(categories(:child_2).id)
470
- assert Category.valid?
471
- end
472
-
473
- def test_valid_with_overlapping_and_rights
474
- assert Category.valid?
475
- categories(:top_level_2)['lft'] = 0
476
- categories(:top_level_2).save
477
- assert !Category.valid?
478
- end
479
-
480
- def test_rebuild
481
- assert Category.valid?
482
- before_text = Category.root.to_text
483
- Category.update_all('lft = null, rgt = null')
484
- Category.rebuild!
485
- assert Category.valid?
486
- assert_equal before_text, Category.root.to_text
487
- end
488
-
489
- def test_move_possible_for_sibling
490
- assert categories(:child_2).move_possible?(categories(:child_1))
491
- end
492
-
493
- def test_move_not_possible_to_self
494
- assert !categories(:top_level).move_possible?(categories(:top_level))
495
- end
496
-
497
- def test_move_not_possible_to_parent
498
- categories(:top_level).descendants.each do |descendant|
499
- assert !categories(:top_level).move_possible?(descendant)
500
- assert descendant.move_possible?(categories(:top_level))
501
- end
502
- end
503
-
504
- def test_is_or_is_ancestor_of?
505
- [:child_1, :child_2, :child_2_1, :child_3].each do |c|
506
- assert categories(:top_level).is_or_is_ancestor_of?(categories(c))
507
- end
508
- assert !categories(:top_level).is_or_is_ancestor_of?(categories(:top_level_2))
509
- end
510
-
511
- def test_left_and_rights_valid_with_blank_left
512
- assert Category.left_and_rights_valid?
513
- categories(:child_2)[:lft] = nil
514
- categories(:child_2).save(false)
515
- assert !Category.left_and_rights_valid?
516
- end
517
-
518
- def test_left_and_rights_valid_with_blank_right
519
- assert Category.left_and_rights_valid?
520
- categories(:child_2)[:rgt] = nil
521
- categories(:child_2).save(false)
522
- assert !Category.left_and_rights_valid?
523
- end
524
-
525
- def test_left_and_rights_valid_with_equal
526
- assert Category.left_and_rights_valid?
527
- categories(:top_level_2)[:lft] = categories(:top_level_2)[:rgt]
528
- categories(:top_level_2).save(false)
529
- assert !Category.left_and_rights_valid?
530
- end
531
-
532
- def test_left_and_rights_valid_with_left_equal_to_parent
533
- assert Category.left_and_rights_valid?
534
- categories(:child_2)[:lft] = categories(:top_level)[:lft]
535
- categories(:child_2).save(false)
536
- assert !Category.left_and_rights_valid?
537
- end
538
-
539
- def test_left_and_rights_valid_with_right_equal_to_parent
540
- assert Category.left_and_rights_valid?
541
- categories(:child_2)[:rgt] = categories(:top_level)[:rgt]
542
- categories(:child_2).save(false)
543
- assert !Category.left_and_rights_valid?
544
- end
545
-
546
- def test_moving_dirty_objects_doesnt_invalidate_tree
547
- r1 = Category.create
548
- r2 = Category.create
549
- r3 = Category.create
550
- r4 = Category.create
551
- nodes = [r1, r2, r3, r4]
552
-
553
- r2.move_to_child_of(r1)
554
- assert Category.valid?
555
-
556
- r3.move_to_child_of(r1)
557
- assert Category.valid?
558
-
559
- r4.move_to_child_of(r2)
560
- assert Category.valid?
561
- end
562
-
563
- def test_multi_scoped_no_duplicates_for_columns?
564
- assert_nothing_raised do
565
- Note.no_duplicates_for_columns?
566
- end
567
- end
568
-
569
- def test_multi_scoped_all_roots_valid?
570
- assert_nothing_raised do
571
- Note.all_roots_valid?
572
- end
573
- end
574
-
575
- def test_multi_scoped
576
- note1 = Note.create!(:body => "A", :notable_id => 2, :notable_type => 'Category')
577
- note2 = Note.create!(:body => "B", :notable_id => 2, :notable_type => 'Category')
578
- note3 = Note.create!(:body => "C", :notable_id => 2, :notable_type => 'Default')
579
-
580
- assert_equal [note1, note2], note1.self_and_siblings
581
- assert_equal [note3], note3.self_and_siblings
582
- end
583
-
584
- def test_multi_scoped_rebuild
585
- root = Note.create!(:body => "A", :notable_id => 3, :notable_type => 'Category')
586
- child1 = Note.create!(:body => "B", :notable_id => 3, :notable_type => 'Category')
587
- child2 = Note.create!(:body => "C", :notable_id => 3, :notable_type => 'Category')
588
-
589
- child1.move_to_child_of root
590
- child2.move_to_child_of root
591
-
592
- Note.update_all('lft = null, rgt = null')
593
- Note.rebuild!
594
-
595
- assert_equal Note.roots.find_by_body('A'), root
596
- assert_equal [child1, child2], Note.roots.find_by_body('A').children
597
- end
598
-
599
- def test_same_scope_with_multi_scopes
600
- assert_nothing_raised do
601
- notes(:scope1).same_scope?(notes(:child_1))
602
- end
603
- assert notes(:scope1).same_scope?(notes(:child_1))
604
- assert notes(:child_1).same_scope?(notes(:scope1))
605
- assert !notes(:scope1).same_scope?(notes(:scope2))
606
- end
607
-
608
- def test_quoting_of_multi_scope_column_names
609
- assert_equal ["\"notable_id\"", "\"notable_type\""], Note.quoted_scope_column_names
610
- end
611
-
612
- def test_equal_in_same_scope
613
- assert_equal notes(:scope1), notes(:scope1)
614
- assert_not_equal notes(:scope1), notes(:child_1)
615
- end
616
-
617
- def test_equal_in_different_scopes
618
- assert_not_equal notes(:scope1), notes(:scope2)
619
- end
620
-
621
- def test_delete_does_not_invalidate
622
- Category.acts_as_nested_set_options[:dependent] = :delete
623
- categories(:child_2).destroy
624
- assert Category.valid?
625
- end
626
-
627
- def test_destroy_does_not_invalidate
628
- Category.acts_as_nested_set_options[:dependent] = :destroy
629
- categories(:child_2).destroy
630
- assert Category.valid?
631
- end
632
-
633
- def test_destroy_multiple_times_does_not_invalidate
634
- Category.acts_as_nested_set_options[:dependent] = :destroy
635
- categories(:child_2).destroy
636
- categories(:child_2).destroy
637
- assert Category.valid?
638
- end
639
-
640
- def test_assigning_parent_id_on_create
641
- category = Category.create!(:name => "Child", :parent_id => categories(:child_2).id)
642
- assert_equal categories(:child_2), category.parent
643
- assert_equal categories(:child_2).id, category.parent_id
644
- assert_not_nil category.left
645
- assert_not_nil category.right
646
- assert Category.valid?
647
- end
648
-
649
- def test_assigning_parent_on_create
650
- category = Category.create!(:name => "Child", :parent => categories(:child_2))
651
- assert_equal categories(:child_2), category.parent
652
- assert_equal categories(:child_2).id, category.parent_id
653
- assert_not_nil category.left
654
- assert_not_nil category.right
655
- assert Category.valid?
656
- end
657
-
658
- def test_assigning_parent_id_to_nil_on_create
659
- category = Category.create!(:name => "New Root", :parent_id => nil)
660
- assert_nil category.parent
661
- assert_nil category.parent_id
662
- assert_not_nil category.left
663
- assert_not_nil category.right
664
- assert Category.valid?
665
- end
666
-
667
- def test_assigning_parent_id_on_update
668
- category = categories(:child_2_1)
669
- category.parent_id = categories(:child_3).id
670
- category.save
671
- assert_equal categories(:child_3), category.parent
672
- assert_equal categories(:child_3).id, category.parent_id
673
- assert Category.valid?
674
- end
675
-
676
- def test_assigning_parent_on_update
677
- category = categories(:child_2_1)
678
- category.parent = categories(:child_3)
679
- category.save
680
- assert_equal categories(:child_3), category.parent
681
- assert_equal categories(:child_3).id, category.parent_id
682
- assert Category.valid?
683
- end
684
-
685
- def test_assigning_parent_id_to_nil_on_update
686
- category = categories(:child_2_1)
687
- category.parent_id = nil
688
- category.save
689
- assert_nil category.parent
690
- assert_nil category.parent_id
691
- assert Category.valid?
692
- end
693
-
694
- def test_creating_child_from_parent
695
- category = categories(:child_2).children.create!(:name => "Child")
696
- assert_equal categories(:child_2), category.parent
697
- assert_equal categories(:child_2).id, category.parent_id
698
- assert_not_nil category.left
699
- assert_not_nil category.right
700
- assert Category.valid?
701
- end
702
-
703
- def check_structure(entries, structure)
704
- structure = structure.dup
705
- Category.each_with_level(entries) do |category, level|
706
- expected_level, expected_name = structure.shift
707
- assert_equal expected_name, category.name, "wrong category"
708
- assert_equal expected_level, level, "wrong level for #{category.name}"
709
- end
710
- end
711
-
712
- def test_each_with_level
713
- levels = [
714
- [0, "Top Level"],
715
- [1, "Child 1"],
716
- [1, "Child 2"],
717
- [2, "Child 2.1"],
718
- [1, "Child 3" ]]
719
-
720
- check_structure(Category.root.self_and_descendants, levels)
721
-
722
- # test some deeper structures
723
- category = Category.find_by_name("Child 1")
724
- c1 = Category.new(:name => "Child 1.1")
725
- c2 = Category.new(:name => "Child 1.1.1")
726
- c3 = Category.new(:name => "Child 1.1.1.1")
727
- c4 = Category.new(:name => "Child 1.2")
728
- [c1, c2, c3, c4].each(&:save!)
729
-
730
- c1.move_to_child_of(category)
731
- c2.move_to_child_of(c1)
732
- c3.move_to_child_of(c2)
733
- c4.move_to_child_of(category)
734
-
735
- levels = [
736
- [0, "Top Level"],
737
- [1, "Child 1"],
738
- [2, "Child 1.1"],
739
- [3, "Child 1.1.1"],
740
- [4, "Child 1.1.1.1"],
741
- [2, "Child 1.2"],
742
- [1, "Child 2"],
743
- [2, "Child 2.1"],
744
- [1, "Child 3" ]]
745
-
746
- check_structure(Category.root.self_and_descendants, levels)
747
- end
748
-
749
- def test_model_with_attr_accessible
750
- model = Class.new(ActiveRecord::Base)
751
- model.set_table_name 'categories'
752
- model.attr_accessible :name
753
- assert_nothing_raised do
754
- model.acts_as_nested_set
755
- model.new(:name => 'foo')
756
- end
757
- end
758
-
759
- def test_before_move_callback
760
- @called = false
761
- Category.before_move do |record|
762
- @called = true
763
- end
764
- categories(:child_2).move_to_root
765
- assert @called
766
- ensure
767
- Category.before_move_callback_chain.pop
768
- end
769
-
770
- def test_before_move_callback_returning_false_stops_move
771
- Category.before_move do
772
- return false
773
- end
774
- assert !categories(:child_3).move_to_root
775
- assert !categories(:child_3).root?
776
- ensure
777
- Category.before_move_callback_chain.pop
778
- end
779
-
780
- def test_before_move_callback_returning_false_halts_save
781
- Category.before_move do
782
- return false
783
- end
784
- categories(:child_3).parent_id = nil
785
- assert !categories(:child_3).save
786
- ensure
787
- Category.before_move_callback_chain.pop
788
- end
789
-
790
- def test_calls_after_save_when_moving
791
- @called = false
792
- Category.after_save do
793
- @called = true
794
- end
795
- categories(:child_3).parent = categories(:child_2)
796
- assert categories(:child_3).save
797
- assert @called
798
- ensure
799
- Category.after_save_callback_chain.pop
800
- end
801
- end