annotate .svn/pristine/ee/ee3e0d946d377acd7e76d501db77540601a10ca7.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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