annotate .svn/pristine/ee/ee3e0d946d377acd7e76d501db77540601a10ca7.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 require 'spec_helper'
Chris@1296 2
Chris@1296 3 describe "AwesomeNestedSet" do
Chris@1296 4 before(:all) do
Chris@1296 5 self.class.fixtures :categories, :departments, :notes, :things, :brokens
Chris@1296 6 end
Chris@1296 7
Chris@1296 8 describe "defaults" do
Chris@1296 9 it "should have left_column_default" do
Chris@1296 10 Default.acts_as_nested_set_options[:left_column].should == 'lft'
Chris@1296 11 end
Chris@1296 12
Chris@1296 13 it "should have right_column_default" do
Chris@1296 14 Default.acts_as_nested_set_options[:right_column].should == 'rgt'
Chris@1296 15 end
Chris@1296 16
Chris@1296 17 it "should have parent_column_default" do
Chris@1296 18 Default.acts_as_nested_set_options[:parent_column].should == 'parent_id'
Chris@1296 19 end
Chris@1296 20
Chris@1296 21 it "should have scope_default" do
Chris@1296 22 Default.acts_as_nested_set_options[:scope].should be_nil
Chris@1296 23 end
Chris@1296 24
Chris@1296 25 it "should have left_column_name" do
Chris@1296 26 Default.left_column_name.should == 'lft'
Chris@1296 27 Default.new.left_column_name.should == 'lft'
Chris@1296 28 RenamedColumns.left_column_name.should == 'red'
Chris@1296 29 RenamedColumns.new.left_column_name.should == 'red'
Chris@1296 30 end
Chris@1296 31
Chris@1296 32 it "should have right_column_name" do
Chris@1296 33 Default.right_column_name.should == 'rgt'
Chris@1296 34 Default.new.right_column_name.should == 'rgt'
Chris@1296 35 RenamedColumns.right_column_name.should == 'black'
Chris@1296 36 RenamedColumns.new.right_column_name.should == 'black'
Chris@1296 37 end
Chris@1296 38
Chris@1296 39 it "should have parent_column_name" do
Chris@1296 40 Default.parent_column_name.should == 'parent_id'
Chris@1296 41 Default.new.parent_column_name.should == 'parent_id'
Chris@1296 42 RenamedColumns.parent_column_name.should == 'mother_id'
Chris@1296 43 RenamedColumns.new.parent_column_name.should == 'mother_id'
Chris@1296 44 end
Chris@1296 45 end
Chris@1296 46
Chris@1296 47 it "creation_with_altered_column_names" do
Chris@1296 48 lambda {
Chris@1296 49 RenamedColumns.create!()
Chris@1296 50 }.should_not raise_exception
Chris@1296 51 end
Chris@1296 52
Chris@1296 53 it "creation when existing record has nil left column" do
Chris@1296 54 assert_nothing_raised do
Chris@1296 55 Broken.create!
Chris@1296 56 end
Chris@1296 57 end
Chris@1296 58
Chris@1296 59 it "quoted_left_column_name" do
Chris@1296 60 quoted = Default.connection.quote_column_name('lft')
Chris@1296 61 Default.quoted_left_column_name.should == quoted
Chris@1296 62 Default.new.quoted_left_column_name.should == quoted
Chris@1296 63 end
Chris@1296 64
Chris@1296 65 it "quoted_right_column_name" do
Chris@1296 66 quoted = Default.connection.quote_column_name('rgt')
Chris@1296 67 Default.quoted_right_column_name.should == quoted
Chris@1296 68 Default.new.quoted_right_column_name.should == quoted
Chris@1296 69 end
Chris@1296 70
Chris@1296 71 it "left_column_protected_from_assignment" do
Chris@1296 72 lambda {
Chris@1296 73 Category.new.lft = 1
Chris@1296 74 }.should raise_exception(ActiveRecord::ActiveRecordError)
Chris@1296 75 end
Chris@1296 76
Chris@1296 77 it "right_column_protected_from_assignment" do
Chris@1296 78 lambda {
Chris@1296 79 Category.new.rgt = 1
Chris@1296 80 }.should raise_exception(ActiveRecord::ActiveRecordError)
Chris@1296 81 end
Chris@1296 82
Chris@1296 83 it "scoped_appends_id" do
Chris@1296 84 ScopedCategory.acts_as_nested_set_options[:scope].should == :organization_id
Chris@1296 85 end
Chris@1296 86
Chris@1296 87 it "roots_class_method" do
Chris@1296 88 Category.roots.should == Category.find_all_by_parent_id(nil)
Chris@1296 89 end
Chris@1296 90
Chris@1296 91 it "root_class_method" do
Chris@1296 92 Category.root.should == categories(:top_level)
Chris@1296 93 end
Chris@1296 94
Chris@1296 95 it "root" do
Chris@1296 96 categories(:child_3).root.should == categories(:top_level)
Chris@1296 97 end
Chris@1296 98
Chris@1296 99 it "root?" do
Chris@1296 100 categories(:top_level).root?.should be_true
Chris@1296 101 categories(:top_level_2).root?.should be_true
Chris@1296 102 end
Chris@1296 103
Chris@1296 104 it "leaves_class_method" do
Chris@1296 105 Category.find(:all, :conditions => "#{Category.right_column_name} - #{Category.left_column_name} = 1").should == Category.leaves
Chris@1296 106 Category.leaves.count.should == 4
Chris@1296 107 Category.leaves.should include(categories(:child_1))
Chris@1296 108 Category.leaves.should include(categories(:child_2_1))
Chris@1296 109 Category.leaves.should include(categories(:child_3))
Chris@1296 110 Category.leaves.should include(categories(:top_level_2))
Chris@1296 111 end
Chris@1296 112
Chris@1296 113 it "leaf" do
Chris@1296 114 categories(:child_1).leaf?.should be_true
Chris@1296 115 categories(:child_2_1).leaf?.should be_true
Chris@1296 116 categories(:child_3).leaf?.should be_true
Chris@1296 117 categories(:top_level_2).leaf?.should be_true
Chris@1296 118
Chris@1296 119 categories(:top_level).leaf?.should be_false
Chris@1296 120 categories(:child_2).leaf?.should be_false
Chris@1296 121 Category.new.leaf?.should be_false
Chris@1296 122 end
Chris@1296 123
Chris@1296 124
Chris@1296 125 it "parent" do
Chris@1296 126 categories(:child_2_1).parent.should == categories(:child_2)
Chris@1296 127 end
Chris@1296 128
Chris@1296 129 it "self_and_ancestors" do
Chris@1296 130 child = categories(:child_2_1)
Chris@1296 131 self_and_ancestors = [categories(:top_level), categories(:child_2), child]
Chris@1296 132 self_and_ancestors.should == child.self_and_ancestors
Chris@1296 133 end
Chris@1296 134
Chris@1296 135 it "ancestors" do
Chris@1296 136 child = categories(:child_2_1)
Chris@1296 137 ancestors = [categories(:top_level), categories(:child_2)]
Chris@1296 138 ancestors.should == child.ancestors
Chris@1296 139 end
Chris@1296 140
Chris@1296 141 it "self_and_siblings" do
Chris@1296 142 child = categories(:child_2)
Chris@1296 143 self_and_siblings = [categories(:child_1), child, categories(:child_3)]
Chris@1296 144 self_and_siblings.should == child.self_and_siblings
Chris@1296 145 lambda do
Chris@1296 146 tops = [categories(:top_level), categories(:top_level_2)]
Chris@1296 147 assert_equal tops, categories(:top_level).self_and_siblings
Chris@1296 148 end.should_not raise_exception
Chris@1296 149 end
Chris@1296 150
Chris@1296 151 it "siblings" do
Chris@1296 152 child = categories(:child_2)
Chris@1296 153 siblings = [categories(:child_1), categories(:child_3)]
Chris@1296 154 siblings.should == child.siblings
Chris@1296 155 end
Chris@1296 156
Chris@1296 157 it "leaves" do
Chris@1296 158 leaves = [categories(:child_1), categories(:child_2_1), categories(:child_3)]
Chris@1296 159 categories(:top_level).leaves.should == leaves
Chris@1296 160 end
Chris@1296 161
Chris@1296 162 it "level" do
Chris@1296 163 categories(:top_level).level.should == 0
Chris@1296 164 categories(:child_1).level.should == 1
Chris@1296 165 categories(:child_2_1).level.should == 2
Chris@1296 166 end
Chris@1296 167
Chris@1296 168 it "has_children?" do
Chris@1296 169 categories(:child_2_1).children.empty?.should be_true
Chris@1296 170 categories(:child_2).children.empty?.should be_false
Chris@1296 171 categories(:top_level).children.empty?.should be_false
Chris@1296 172 end
Chris@1296 173
Chris@1296 174 it "self_and_descendents" do
Chris@1296 175 parent = categories(:top_level)
Chris@1296 176 self_and_descendants = [parent, categories(:child_1), categories(:child_2),
Chris@1296 177 categories(:child_2_1), categories(:child_3)]
Chris@1296 178 self_and_descendants.should == parent.self_and_descendants
Chris@1296 179 self_and_descendants.count.should == parent.self_and_descendants.count
Chris@1296 180 end
Chris@1296 181
Chris@1296 182 it "descendents" do
Chris@1296 183 lawyers = Category.create!(:name => "lawyers")
Chris@1296 184 us = Category.create!(:name => "United States")
Chris@1296 185 us.move_to_child_of(lawyers)
Chris@1296 186 patent = Category.create!(:name => "Patent Law")
Chris@1296 187 patent.move_to_child_of(us)
Chris@1296 188 lawyers.reload
Chris@1296 189
Chris@1296 190 lawyers.children.size.should == 1
Chris@1296 191 us.children.size.should == 1
Chris@1296 192 lawyers.descendants.size.should == 2
Chris@1296 193 end
Chris@1296 194
Chris@1296 195 it "self_and_descendents" do
Chris@1296 196 parent = categories(:top_level)
Chris@1296 197 descendants = [categories(:child_1), categories(:child_2),
Chris@1296 198 categories(:child_2_1), categories(:child_3)]
Chris@1296 199 descendants.should == parent.descendants
Chris@1296 200 end
Chris@1296 201
Chris@1296 202 it "children" do
Chris@1296 203 category = categories(:top_level)
Chris@1296 204 category.children.each {|c| category.id.should == c.parent_id }
Chris@1296 205 end
Chris@1296 206
Chris@1296 207 it "order_of_children" do
Chris@1296 208 categories(:child_2).move_left
Chris@1296 209 categories(:child_2).should == categories(:top_level).children[0]
Chris@1296 210 categories(:child_1).should == categories(:top_level).children[1]
Chris@1296 211 categories(:child_3).should == categories(:top_level).children[2]
Chris@1296 212 end
Chris@1296 213
Chris@1296 214 it "is_or_is_ancestor_of?" do
Chris@1296 215 categories(:top_level).is_or_is_ancestor_of?(categories(:child_1)).should be_true
Chris@1296 216 categories(:top_level).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true
Chris@1296 217 categories(:child_2).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true
Chris@1296 218 categories(:child_2_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false
Chris@1296 219 categories(:child_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false
Chris@1296 220 categories(:child_1).is_or_is_ancestor_of?(categories(:child_1)).should be_true
Chris@1296 221 end
Chris@1296 222
Chris@1296 223 it "is_ancestor_of?" do
Chris@1296 224 categories(:top_level).is_ancestor_of?(categories(:child_1)).should be_true
Chris@1296 225 categories(:top_level).is_ancestor_of?(categories(:child_2_1)).should be_true
Chris@1296 226 categories(:child_2).is_ancestor_of?(categories(:child_2_1)).should be_true
Chris@1296 227 categories(:child_2_1).is_ancestor_of?(categories(:child_2)).should be_false
Chris@1296 228 categories(:child_1).is_ancestor_of?(categories(:child_2)).should be_false
Chris@1296 229 categories(:child_1).is_ancestor_of?(categories(:child_1)).should be_false
Chris@1296 230 end
Chris@1296 231
Chris@1296 232 it "is_or_is_ancestor_of_with_scope" do
Chris@1296 233 root = ScopedCategory.root
Chris@1296 234 child = root.children.first
Chris@1296 235 root.is_or_is_ancestor_of?(child).should be_true
Chris@1296 236 child.update_attribute :organization_id, 'different'
Chris@1296 237 root.is_or_is_ancestor_of?(child).should be_false
Chris@1296 238 end
Chris@1296 239
Chris@1296 240 it "is_or_is_descendant_of?" do
Chris@1296 241 categories(:child_1).is_or_is_descendant_of?(categories(:top_level)).should be_true
Chris@1296 242 categories(:child_2_1).is_or_is_descendant_of?(categories(:top_level)).should be_true
Chris@1296 243 categories(:child_2_1).is_or_is_descendant_of?(categories(:child_2)).should be_true
Chris@1296 244 categories(:child_2).is_or_is_descendant_of?(categories(:child_2_1)).should be_false
Chris@1296 245 categories(:child_2).is_or_is_descendant_of?(categories(:child_1)).should be_false
Chris@1296 246 categories(:child_1).is_or_is_descendant_of?(categories(:child_1)).should be_true
Chris@1296 247 end
Chris@1296 248
Chris@1296 249 it "is_descendant_of?" do
Chris@1296 250 categories(:child_1).is_descendant_of?(categories(:top_level)).should be_true
Chris@1296 251 categories(:child_2_1).is_descendant_of?(categories(:top_level)).should be_true
Chris@1296 252 categories(:child_2_1).is_descendant_of?(categories(:child_2)).should be_true
Chris@1296 253 categories(:child_2).is_descendant_of?(categories(:child_2_1)).should be_false
Chris@1296 254 categories(:child_2).is_descendant_of?(categories(:child_1)).should be_false
Chris@1296 255 categories(:child_1).is_descendant_of?(categories(:child_1)).should be_false
Chris@1296 256 end
Chris@1296 257
Chris@1296 258 it "is_or_is_descendant_of_with_scope" do
Chris@1296 259 root = ScopedCategory.root
Chris@1296 260 child = root.children.first
Chris@1296 261 child.is_or_is_descendant_of?(root).should be_true
Chris@1296 262 child.update_attribute :organization_id, 'different'
Chris@1296 263 child.is_or_is_descendant_of?(root).should be_false
Chris@1296 264 end
Chris@1296 265
Chris@1296 266 it "same_scope?" do
Chris@1296 267 root = ScopedCategory.root
Chris@1296 268 child = root.children.first
Chris@1296 269 child.same_scope?(root).should be_true
Chris@1296 270 child.update_attribute :organization_id, 'different'
Chris@1296 271 child.same_scope?(root).should be_false
Chris@1296 272 end
Chris@1296 273
Chris@1296 274 it "left_sibling" do
Chris@1296 275 categories(:child_1).should == categories(:child_2).left_sibling
Chris@1296 276 categories(:child_2).should == categories(:child_3).left_sibling
Chris@1296 277 end
Chris@1296 278
Chris@1296 279 it "left_sibling_of_root" do
Chris@1296 280 categories(:top_level).left_sibling.should be_nil
Chris@1296 281 end
Chris@1296 282
Chris@1296 283 it "left_sibling_without_siblings" do
Chris@1296 284 categories(:child_2_1).left_sibling.should be_nil
Chris@1296 285 end
Chris@1296 286
Chris@1296 287 it "left_sibling_of_leftmost_node" do
Chris@1296 288 categories(:child_1).left_sibling.should be_nil
Chris@1296 289 end
Chris@1296 290
Chris@1296 291 it "right_sibling" do
Chris@1296 292 categories(:child_3).should == categories(:child_2).right_sibling
Chris@1296 293 categories(:child_2).should == categories(:child_1).right_sibling
Chris@1296 294 end
Chris@1296 295
Chris@1296 296 it "right_sibling_of_root" do
Chris@1296 297 categories(:top_level_2).should == categories(:top_level).right_sibling
Chris@1296 298 categories(:top_level_2).right_sibling.should be_nil
Chris@1296 299 end
Chris@1296 300
Chris@1296 301 it "right_sibling_without_siblings" do
Chris@1296 302 categories(:child_2_1).right_sibling.should be_nil
Chris@1296 303 end
Chris@1296 304
Chris@1296 305 it "right_sibling_of_rightmost_node" do
Chris@1296 306 categories(:child_3).right_sibling.should be_nil
Chris@1296 307 end
Chris@1296 308
Chris@1296 309 it "move_left" do
Chris@1296 310 categories(:child_2).move_left
Chris@1296 311 categories(:child_2).left_sibling.should be_nil
Chris@1296 312 categories(:child_1).should == categories(:child_2).right_sibling
Chris@1296 313 Category.valid?.should be_true
Chris@1296 314 end
Chris@1296 315
Chris@1296 316 it "move_right" do
Chris@1296 317 categories(:child_2).move_right
Chris@1296 318 categories(:child_2).right_sibling.should be_nil
Chris@1296 319 categories(:child_3).should == categories(:child_2).left_sibling
Chris@1296 320 Category.valid?.should be_true
Chris@1296 321 end
Chris@1296 322
Chris@1296 323 it "move_to_left_of" do
Chris@1296 324 categories(:child_3).move_to_left_of(categories(:child_1))
Chris@1296 325 categories(:child_3).left_sibling.should be_nil
Chris@1296 326 categories(:child_1).should == categories(:child_3).right_sibling
Chris@1296 327 Category.valid?.should be_true
Chris@1296 328 end
Chris@1296 329
Chris@1296 330 it "move_to_right_of" do
Chris@1296 331 categories(:child_1).move_to_right_of(categories(:child_3))
Chris@1296 332 categories(:child_1).right_sibling.should be_nil
Chris@1296 333 categories(:child_3).should == categories(:child_1).left_sibling
Chris@1296 334 Category.valid?.should be_true
Chris@1296 335 end
Chris@1296 336
Chris@1296 337 it "move_to_root" do
Chris@1296 338 categories(:child_2).move_to_root
Chris@1296 339 categories(:child_2).parent.should be_nil
Chris@1296 340 categories(:child_2).level.should == 0
Chris@1296 341 categories(:child_2_1).level.should == 1
Chris@1296 342 categories(:child_2).left.should == 1
Chris@1296 343 categories(:child_2).right.should == 4
Chris@1296 344 Category.valid?.should be_true
Chris@1296 345 end
Chris@1296 346
Chris@1296 347 it "move_to_child_of" do
Chris@1296 348 categories(:child_1).move_to_child_of(categories(:child_3))
Chris@1296 349 categories(:child_3).id.should == categories(:child_1).parent_id
Chris@1296 350 Category.valid?.should be_true
Chris@1296 351 end
Chris@1296 352
Chris@1296 353 it "move_to_child_of_appends_to_end" do
Chris@1296 354 child = Category.create! :name => 'New Child'
Chris@1296 355 child.move_to_child_of categories(:top_level)
Chris@1296 356 child.should == categories(:top_level).children.last
Chris@1296 357 end
Chris@1296 358
Chris@1296 359 it "subtree_move_to_child_of" do
Chris@1296 360 categories(:child_2).left.should == 4
Chris@1296 361 categories(:child_2).right.should == 7
Chris@1296 362
Chris@1296 363 categories(:child_1).left.should == 2
Chris@1296 364 categories(:child_1).right.should == 3
Chris@1296 365
Chris@1296 366 categories(:child_2).move_to_child_of(categories(:child_1))
Chris@1296 367 Category.valid?.should be_true
Chris@1296 368 categories(:child_1).id.should == categories(:child_2).parent_id
Chris@1296 369
Chris@1296 370 categories(:child_2).left.should == 3
Chris@1296 371 categories(:child_2).right.should == 6
Chris@1296 372 categories(:child_1).left.should == 2
Chris@1296 373 categories(:child_1).right.should == 7
Chris@1296 374 end
Chris@1296 375
Chris@1296 376 it "slightly_difficult_move_to_child_of" do
Chris@1296 377 categories(:top_level_2).left.should == 11
Chris@1296 378 categories(:top_level_2).right.should == 12
Chris@1296 379
Chris@1296 380 # create a new top-level node and move single-node top-level tree inside it.
Chris@1296 381 new_top = Category.create(:name => 'New Top')
Chris@1296 382 new_top.left.should == 13
Chris@1296 383 new_top.right.should == 14
Chris@1296 384
Chris@1296 385 categories(:top_level_2).move_to_child_of(new_top)
Chris@1296 386
Chris@1296 387 Category.valid?.should be_true
Chris@1296 388 new_top.id.should == categories(:top_level_2).parent_id
Chris@1296 389
Chris@1296 390 categories(:top_level_2).left.should == 12
Chris@1296 391 categories(:top_level_2).right.should == 13
Chris@1296 392 new_top.left.should == 11
Chris@1296 393 new_top.right.should == 14
Chris@1296 394 end
Chris@1296 395
Chris@1296 396 it "difficult_move_to_child_of" do
Chris@1296 397 categories(:top_level).left.should == 1
Chris@1296 398 categories(:top_level).right.should == 10
Chris@1296 399 categories(:child_2_1).left.should == 5
Chris@1296 400 categories(:child_2_1).right.should == 6
Chris@1296 401
Chris@1296 402 # create a new top-level node and move an entire top-level tree inside it.
Chris@1296 403 new_top = Category.create(:name => 'New Top')
Chris@1296 404 categories(:top_level).move_to_child_of(new_top)
Chris@1296 405 categories(:child_2_1).reload
Chris@1296 406 Category.valid?.should be_true
Chris@1296 407 new_top.id.should == categories(:top_level).parent_id
Chris@1296 408
Chris@1296 409 categories(:top_level).left.should == 4
Chris@1296 410 categories(:top_level).right.should == 13
Chris@1296 411 categories(:child_2_1).left.should == 8
Chris@1296 412 categories(:child_2_1).right.should == 9
Chris@1296 413 end
Chris@1296 414
Chris@1296 415 #rebuild swaps the position of the 2 children when added using move_to_child twice onto same parent
Chris@1296 416 it "move_to_child_more_than_once_per_parent_rebuild" do
Chris@1296 417 root1 = Category.create(:name => 'Root1')
Chris@1296 418 root2 = Category.create(:name => 'Root2')
Chris@1296 419 root3 = Category.create(:name => 'Root3')
Chris@1296 420
Chris@1296 421 root2.move_to_child_of root1
Chris@1296 422 root3.move_to_child_of root1
Chris@1296 423
Chris@1296 424 output = Category.roots.last.to_text
Chris@1296 425 Category.update_all('lft = null, rgt = null')
Chris@1296 426 Category.rebuild!
Chris@1296 427
Chris@1296 428 Category.roots.last.to_text.should == output
Chris@1296 429 end
Chris@1296 430
Chris@1296 431 # doing move_to_child twice onto same parent from the furthest right first
Chris@1296 432 it "move_to_child_more_than_once_per_parent_outside_in" do
Chris@1296 433 node1 = Category.create(:name => 'Node-1')
Chris@1296 434 node2 = Category.create(:name => 'Node-2')
Chris@1296 435 node3 = Category.create(:name => 'Node-3')
Chris@1296 436
Chris@1296 437 node2.move_to_child_of node1
Chris@1296 438 node3.move_to_child_of node1
Chris@1296 439
Chris@1296 440 output = Category.roots.last.to_text
Chris@1296 441 Category.update_all('lft = null, rgt = null')
Chris@1296 442 Category.rebuild!
Chris@1296 443
Chris@1296 444 Category.roots.last.to_text.should == output
Chris@1296 445 end
Chris@1296 446
Chris@1296 447 it "should be able to rebuild without validating each record" do
Chris@1296 448 root1 = Category.create(:name => 'Root1')
Chris@1296 449 root2 = Category.create(:name => 'Root2')
Chris@1296 450 root3 = Category.create(:name => 'Root3')
Chris@1296 451
Chris@1296 452 root2.move_to_child_of root1
Chris@1296 453 root3.move_to_child_of root1
Chris@1296 454
Chris@1296 455 root2.name = nil
Chris@1296 456 root2.save!(:validate => false)
Chris@1296 457
Chris@1296 458 output = Category.roots.last.to_text
Chris@1296 459 Category.update_all('lft = null, rgt = null')
Chris@1296 460 Category.rebuild!(false)
Chris@1296 461
Chris@1296 462 Category.roots.last.to_text.should == output
Chris@1296 463 end
Chris@1296 464
Chris@1296 465 it "valid_with_null_lefts" do
Chris@1296 466 Category.valid?.should be_true
Chris@1296 467 Category.update_all('lft = null')
Chris@1296 468 Category.valid?.should be_false
Chris@1296 469 end
Chris@1296 470
Chris@1296 471 it "valid_with_null_rights" do
Chris@1296 472 Category.valid?.should be_true
Chris@1296 473 Category.update_all('rgt = null')
Chris@1296 474 Category.valid?.should be_false
Chris@1296 475 end
Chris@1296 476
Chris@1296 477 it "valid_with_missing_intermediate_node" do
Chris@1296 478 # Even though child_2_1 will still exist, it is a sign of a sloppy delete, not an invalid tree.
Chris@1296 479 Category.valid?.should be_true
Chris@1296 480 Category.delete(categories(:child_2).id)
Chris@1296 481 Category.valid?.should be_true
Chris@1296 482 end
Chris@1296 483
Chris@1296 484 it "valid_with_overlapping_and_rights" do
Chris@1296 485 Category.valid?.should be_true
Chris@1296 486 categories(:top_level_2)['lft'] = 0
Chris@1296 487 categories(:top_level_2).save
Chris@1296 488 Category.valid?.should be_false
Chris@1296 489 end
Chris@1296 490
Chris@1296 491 it "rebuild" do
Chris@1296 492 Category.valid?.should be_true
Chris@1296 493 before_text = Category.root.to_text
Chris@1296 494 Category.update_all('lft = null, rgt = null')
Chris@1296 495 Category.rebuild!
Chris@1296 496 Category.valid?.should be_true
Chris@1296 497 before_text.should == Category.root.to_text
Chris@1296 498 end
Chris@1296 499
Chris@1296 500 it "move_possible_for_sibling" do
Chris@1296 501 categories(:child_2).move_possible?(categories(:child_1)).should be_true
Chris@1296 502 end
Chris@1296 503
Chris@1296 504 it "move_not_possible_to_self" do
Chris@1296 505 categories(:top_level).move_possible?(categories(:top_level)).should be_false
Chris@1296 506 end
Chris@1296 507
Chris@1296 508 it "move_not_possible_to_parent" do
Chris@1296 509 categories(:top_level).descendants.each do |descendant|
Chris@1296 510 categories(:top_level).move_possible?(descendant).should be_false
Chris@1296 511 descendant.move_possible?(categories(:top_level)).should be_true
Chris@1296 512 end
Chris@1296 513 end
Chris@1296 514
Chris@1296 515 it "is_or_is_ancestor_of?" do
Chris@1296 516 [:child_1, :child_2, :child_2_1, :child_3].each do |c|
Chris@1296 517 categories(:top_level).is_or_is_ancestor_of?(categories(c)).should be_true
Chris@1296 518 end
Chris@1296 519 categories(:top_level).is_or_is_ancestor_of?(categories(:top_level_2)).should be_false
Chris@1296 520 end
Chris@1296 521
Chris@1296 522 it "left_and_rights_valid_with_blank_left" do
Chris@1296 523 Category.left_and_rights_valid?.should be_true
Chris@1296 524 categories(:child_2)[:lft] = nil
Chris@1296 525 categories(:child_2).save(:validate => false)
Chris@1296 526 Category.left_and_rights_valid?.should be_false
Chris@1296 527 end
Chris@1296 528
Chris@1296 529 it "left_and_rights_valid_with_blank_right" do
Chris@1296 530 Category.left_and_rights_valid?.should be_true
Chris@1296 531 categories(:child_2)[:rgt] = nil
Chris@1296 532 categories(:child_2).save(:validate => false)
Chris@1296 533 Category.left_and_rights_valid?.should be_false
Chris@1296 534 end
Chris@1296 535
Chris@1296 536 it "left_and_rights_valid_with_equal" do
Chris@1296 537 Category.left_and_rights_valid?.should be_true
Chris@1296 538 categories(:top_level_2)[:lft] = categories(:top_level_2)[:rgt]
Chris@1296 539 categories(:top_level_2).save(:validate => false)
Chris@1296 540 Category.left_and_rights_valid?.should be_false
Chris@1296 541 end
Chris@1296 542
Chris@1296 543 it "left_and_rights_valid_with_left_equal_to_parent" do
Chris@1296 544 Category.left_and_rights_valid?.should be_true
Chris@1296 545 categories(:child_2)[:lft] = categories(:top_level)[:lft]
Chris@1296 546 categories(:child_2).save(:validate => false)
Chris@1296 547 Category.left_and_rights_valid?.should be_false
Chris@1296 548 end
Chris@1296 549
Chris@1296 550 it "left_and_rights_valid_with_right_equal_to_parent" do
Chris@1296 551 Category.left_and_rights_valid?.should be_true
Chris@1296 552 categories(:child_2)[:rgt] = categories(:top_level)[:rgt]
Chris@1296 553 categories(:child_2).save(:validate => false)
Chris@1296 554 Category.left_and_rights_valid?.should be_false
Chris@1296 555 end
Chris@1296 556
Chris@1296 557 it "moving_dirty_objects_doesnt_invalidate_tree" do
Chris@1296 558 r1 = Category.create :name => "Test 1"
Chris@1296 559 r2 = Category.create :name => "Test 2"
Chris@1296 560 r3 = Category.create :name => "Test 3"
Chris@1296 561 r4 = Category.create :name => "Test 4"
Chris@1296 562 nodes = [r1, r2, r3, r4]
Chris@1296 563
Chris@1296 564 r2.move_to_child_of(r1)
Chris@1296 565 Category.valid?.should be_true
Chris@1296 566
Chris@1296 567 r3.move_to_child_of(r1)
Chris@1296 568 Category.valid?.should be_true
Chris@1296 569
Chris@1296 570 r4.move_to_child_of(r2)
Chris@1296 571 Category.valid?.should be_true
Chris@1296 572 end
Chris@1296 573
Chris@1296 574 it "multi_scoped_no_duplicates_for_columns?" do
Chris@1296 575 lambda {
Chris@1296 576 Note.no_duplicates_for_columns?
Chris@1296 577 }.should_not raise_exception
Chris@1296 578 end
Chris@1296 579
Chris@1296 580 it "multi_scoped_all_roots_valid?" do
Chris@1296 581 lambda {
Chris@1296 582 Note.all_roots_valid?
Chris@1296 583 }.should_not raise_exception
Chris@1296 584 end
Chris@1296 585
Chris@1296 586 it "multi_scoped" do
Chris@1296 587 note1 = Note.create!(:body => "A", :notable_id => 2, :notable_type => 'Category')
Chris@1296 588 note2 = Note.create!(:body => "B", :notable_id => 2, :notable_type => 'Category')
Chris@1296 589 note3 = Note.create!(:body => "C", :notable_id => 2, :notable_type => 'Default')
Chris@1296 590
Chris@1296 591 [note1, note2].should == note1.self_and_siblings
Chris@1296 592 [note3].should == note3.self_and_siblings
Chris@1296 593 end
Chris@1296 594
Chris@1296 595 it "multi_scoped_rebuild" do
Chris@1296 596 root = Note.create!(:body => "A", :notable_id => 3, :notable_type => 'Category')
Chris@1296 597 child1 = Note.create!(:body => "B", :notable_id => 3, :notable_type => 'Category')
Chris@1296 598 child2 = Note.create!(:body => "C", :notable_id => 3, :notable_type => 'Category')
Chris@1296 599
Chris@1296 600 child1.move_to_child_of root
Chris@1296 601 child2.move_to_child_of root
Chris@1296 602
Chris@1296 603 Note.update_all('lft = null, rgt = null')
Chris@1296 604 Note.rebuild!
Chris@1296 605
Chris@1296 606 Note.roots.find_by_body('A').should == root
Chris@1296 607 [child1, child2].should == Note.roots.find_by_body('A').children
Chris@1296 608 end
Chris@1296 609
Chris@1296 610 it "same_scope_with_multi_scopes" do
Chris@1296 611 lambda {
Chris@1296 612 notes(:scope1).same_scope?(notes(:child_1))
Chris@1296 613 }.should_not raise_exception
Chris@1296 614 notes(:scope1).same_scope?(notes(:child_1)).should be_true
Chris@1296 615 notes(:child_1).same_scope?(notes(:scope1)).should be_true
Chris@1296 616 notes(:scope1).same_scope?(notes(:scope2)).should be_false
Chris@1296 617 end
Chris@1296 618
Chris@1296 619 it "quoting_of_multi_scope_column_names" do
Chris@1296 620 ["\"notable_id\"", "\"notable_type\""].should == Note.quoted_scope_column_names
Chris@1296 621 end
Chris@1296 622
Chris@1296 623 it "equal_in_same_scope" do
Chris@1296 624 notes(:scope1).should == notes(:scope1)
Chris@1296 625 notes(:scope1).should_not == notes(:child_1)
Chris@1296 626 end
Chris@1296 627
Chris@1296 628 it "equal_in_different_scopes" do
Chris@1296 629 notes(:scope1).should_not == notes(:scope2)
Chris@1296 630 end
Chris@1296 631
Chris@1296 632 it "delete_does_not_invalidate" do
Chris@1296 633 Category.acts_as_nested_set_options[:dependent] = :delete
Chris@1296 634 categories(:child_2).destroy
Chris@1296 635 Category.valid?.should be_true
Chris@1296 636 end
Chris@1296 637
Chris@1296 638 it "destroy_does_not_invalidate" do
Chris@1296 639 Category.acts_as_nested_set_options[:dependent] = :destroy
Chris@1296 640 categories(:child_2).destroy
Chris@1296 641 Category.valid?.should be_true
Chris@1296 642 end
Chris@1296 643
Chris@1296 644 it "destroy_multiple_times_does_not_invalidate" do
Chris@1296 645 Category.acts_as_nested_set_options[:dependent] = :destroy
Chris@1296 646 categories(:child_2).destroy
Chris@1296 647 categories(:child_2).destroy
Chris@1296 648 Category.valid?.should be_true
Chris@1296 649 end
Chris@1296 650
Chris@1296 651 it "assigning_parent_id_on_create" do
Chris@1296 652 category = Category.create!(:name => "Child", :parent_id => categories(:child_2).id)
Chris@1296 653 categories(:child_2).should == category.parent
Chris@1296 654 categories(:child_2).id.should == category.parent_id
Chris@1296 655 category.left.should_not be_nil
Chris@1296 656 category.right.should_not be_nil
Chris@1296 657 Category.valid?.should be_true
Chris@1296 658 end
Chris@1296 659
Chris@1296 660 it "assigning_parent_on_create" do
Chris@1296 661 category = Category.create!(:name => "Child", :parent => categories(:child_2))
Chris@1296 662 categories(:child_2).should == category.parent
Chris@1296 663 categories(:child_2).id.should == category.parent_id
Chris@1296 664 category.left.should_not be_nil
Chris@1296 665 category.right.should_not be_nil
Chris@1296 666 Category.valid?.should be_true
Chris@1296 667 end
Chris@1296 668
Chris@1296 669 it "assigning_parent_id_to_nil_on_create" do
Chris@1296 670 category = Category.create!(:name => "New Root", :parent_id => nil)
Chris@1296 671 category.parent.should be_nil
Chris@1296 672 category.parent_id.should be_nil
Chris@1296 673 category.left.should_not be_nil
Chris@1296 674 category.right.should_not be_nil
Chris@1296 675 Category.valid?.should be_true
Chris@1296 676 end
Chris@1296 677
Chris@1296 678 it "assigning_parent_id_on_update" do
Chris@1296 679 category = categories(:child_2_1)
Chris@1296 680 category.parent_id = categories(:child_3).id
Chris@1296 681 category.save
Chris@1296 682 category.reload
Chris@1296 683 categories(:child_3).reload
Chris@1296 684 categories(:child_3).should == category.parent
Chris@1296 685 categories(:child_3).id.should == category.parent_id
Chris@1296 686 Category.valid?.should be_true
Chris@1296 687 end
Chris@1296 688
Chris@1296 689 it "assigning_parent_on_update" do
Chris@1296 690 category = categories(:child_2_1)
Chris@1296 691 category.parent = categories(:child_3)
Chris@1296 692 category.save
Chris@1296 693 category.reload
Chris@1296 694 categories(:child_3).reload
Chris@1296 695 categories(:child_3).should == category.parent
Chris@1296 696 categories(:child_3).id.should == category.parent_id
Chris@1296 697 Category.valid?.should be_true
Chris@1296 698 end
Chris@1296 699
Chris@1296 700 it "assigning_parent_id_to_nil_on_update" do
Chris@1296 701 category = categories(:child_2_1)
Chris@1296 702 category.parent_id = nil
Chris@1296 703 category.save
Chris@1296 704 category.parent.should be_nil
Chris@1296 705 category.parent_id.should be_nil
Chris@1296 706 Category.valid?.should be_true
Chris@1296 707 end
Chris@1296 708
Chris@1296 709 it "creating_child_from_parent" do
Chris@1296 710 category = categories(:child_2).children.create!(:name => "Child")
Chris@1296 711 categories(:child_2).should == category.parent
Chris@1296 712 categories(:child_2).id.should == category.parent_id
Chris@1296 713 category.left.should_not be_nil
Chris@1296 714 category.right.should_not be_nil
Chris@1296 715 Category.valid?.should be_true
Chris@1296 716 end
Chris@1296 717
Chris@1296 718 def check_structure(entries, structure)
Chris@1296 719 structure = structure.dup
Chris@1296 720 Category.each_with_level(entries) do |category, level|
Chris@1296 721 expected_level, expected_name = structure.shift
Chris@1296 722 expected_name.should == category.name
Chris@1296 723 expected_level.should == level
Chris@1296 724 end
Chris@1296 725 end
Chris@1296 726
Chris@1296 727 it "each_with_level" do
Chris@1296 728 levels = [
Chris@1296 729 [0, "Top Level"],
Chris@1296 730 [1, "Child 1"],
Chris@1296 731 [1, "Child 2"],
Chris@1296 732 [2, "Child 2.1"],
Chris@1296 733 [1, "Child 3" ]]
Chris@1296 734
Chris@1296 735 check_structure(Category.root.self_and_descendants, levels)
Chris@1296 736
Chris@1296 737 # test some deeper structures
Chris@1296 738 category = Category.find_by_name("Child 1")
Chris@1296 739 c1 = Category.new(:name => "Child 1.1")
Chris@1296 740 c2 = Category.new(:name => "Child 1.1.1")
Chris@1296 741 c3 = Category.new(:name => "Child 1.1.1.1")
Chris@1296 742 c4 = Category.new(:name => "Child 1.2")
Chris@1296 743 [c1, c2, c3, c4].each(&:save!)
Chris@1296 744
Chris@1296 745 c1.move_to_child_of(category)
Chris@1296 746 c2.move_to_child_of(c1)
Chris@1296 747 c3.move_to_child_of(c2)
Chris@1296 748 c4.move_to_child_of(category)
Chris@1296 749
Chris@1296 750 levels = [
Chris@1296 751 [0, "Top Level"],
Chris@1296 752 [1, "Child 1"],
Chris@1296 753 [2, "Child 1.1"],
Chris@1296 754 [3, "Child 1.1.1"],
Chris@1296 755 [4, "Child 1.1.1.1"],
Chris@1296 756 [2, "Child 1.2"],
Chris@1296 757 [1, "Child 2"],
Chris@1296 758 [2, "Child 2.1"],
Chris@1296 759 [1, "Child 3" ]]
Chris@1296 760
Chris@1296 761 check_structure(Category.root.self_and_descendants, levels)
Chris@1296 762 end
Chris@1296 763
Chris@1296 764 it "should not error on a model with attr_accessible" do
Chris@1296 765 model = Class.new(ActiveRecord::Base)
Chris@1296 766 model.table_name = 'categories'
Chris@1296 767 model.attr_accessible :name
Chris@1296 768 lambda {
Chris@1296 769 model.acts_as_nested_set
Chris@1296 770 model.new(:name => 'foo')
Chris@1296 771 }.should_not raise_exception
Chris@1296 772 end
Chris@1296 773
Chris@1296 774 describe "before_move_callback" do
Chris@1296 775 it "should fire the callback" do
Chris@1296 776 categories(:child_2).should_receive(:custom_before_move)
Chris@1296 777 categories(:child_2).move_to_root
Chris@1296 778 end
Chris@1296 779
Chris@1296 780 it "should stop move when callback returns false" do
Chris@1296 781 Category.test_allows_move = false
Chris@1296 782 categories(:child_3).move_to_root.should be_false
Chris@1296 783 categories(:child_3).root?.should be_false
Chris@1296 784 end
Chris@1296 785
Chris@1296 786 it "should not halt save actions" do
Chris@1296 787 Category.test_allows_move = false
Chris@1296 788 categories(:child_3).parent_id = nil
Chris@1296 789 categories(:child_3).save.should be_true
Chris@1296 790 end
Chris@1296 791 end
Chris@1296 792
Chris@1296 793 describe "counter_cache" do
Chris@1296 794
Chris@1296 795 it "should allow use of a counter cache for children" do
Chris@1296 796 note1 = things(:parent1)
Chris@1296 797 note1.children.count.should == 2
Chris@1296 798 end
Chris@1296 799
Chris@1296 800 it "should increment the counter cache on create" do
Chris@1296 801 note1 = things(:parent1)
Chris@1296 802 note1.children.count.should == 2
Chris@1296 803 note1[:children_count].should == 2
Chris@1296 804 note1.children.create :body => 'Child 3'
Chris@1296 805 note1.children.count.should == 3
Chris@1296 806 note1.reload
Chris@1296 807 note1[:children_count].should == 3
Chris@1296 808 end
Chris@1296 809
Chris@1296 810 it "should decrement the counter cache on destroy" do
Chris@1296 811 note1 = things(:parent1)
Chris@1296 812 note1.children.count.should == 2
Chris@1296 813 note1[:children_count].should == 2
Chris@1296 814 note1.children.last.destroy
Chris@1296 815 note1.children.count.should == 1
Chris@1296 816 note1.reload
Chris@1296 817 note1[:children_count].should == 1
Chris@1296 818 end
Chris@1296 819 end
Chris@1296 820
Chris@1296 821 describe "association callbacks on children" do
Chris@1296 822 it "should call the appropriate callbacks on the children :has_many association " do
Chris@1296 823 root = DefaultWithCallbacks.create
Chris@1296 824 root.should_not be_new_record
Chris@1296 825
Chris@1296 826 child = root.children.build
Chris@1296 827
Chris@1296 828 root.before_add.should == child
Chris@1296 829 root.after_add.should == child
Chris@1296 830
Chris@1296 831 root.before_remove.should_not == child
Chris@1296 832 root.after_remove.should_not == child
Chris@1296 833
Chris@1296 834 child.save.should be_true
Chris@1296 835 root.children.delete(child).should be_true
Chris@1296 836
Chris@1296 837 root.before_remove.should == child
Chris@1296 838 root.after_remove.should == child
Chris@1296 839 end
Chris@1296 840 end
Chris@1296 841 end