Project

General

Profile

Defect #11508 » NestedSetFix.patch

William Roush, 2012-07-31 06:43

View differences:

app/models/project.rb Mon Jul 30 01:32:31 2012 +0000 → app/models/project.rb Tue Jul 31 00:42:18 2012 -0400
367 367
    end
368 368
    set_parent!(p)
369 369
  end
370
  
371
  def nested_set_rebuild(p)
372
    sibs = (p.nil? ? self.class.roots : p.children)
373
    to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
374
    if to_be_inserted_before
375
      move_to_left_of(to_be_inserted_before)
376
    elsif p.nil?
377
      if sibs.empty?
378
        # move_to_root adds the project in first (ie. left) position
379
        move_to_root
380
      else
381
        move_to_right_of(sibs.last) unless self == sibs.last
382
      end
383
    else
384
      # move_to_child_of adds the project in last (ie.right) position
385
      move_to_child_of(p)
386
    end
387
    Issue.update_versions_from_hierarchy_change(self)
388
  end
370 389

  
371 390
  # Sets the parent of the project
372 391
  # Argument can be either a Project, a String, a Fixnum or nil
......
384 403
      true
385 404
    elsif p.nil? || (p.active? && move_possible?(p))
386 405
      # Insert the project so that target's children or root projects stay alphabetically sorted
387
      sibs = (p.nil? ? self.class.roots : p.children)
388
      to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
389
      if to_be_inserted_before
390
        move_to_left_of(to_be_inserted_before)
391
      elsif p.nil?
392
        if sibs.empty?
393
          # move_to_root adds the project in first (ie. left) position
394
          move_to_root
395
        else
396
          move_to_right_of(sibs.last) unless self == sibs.last
397
        end
398
      else
399
        # move_to_child_of adds the project in last (ie.right) position
400
        move_to_child_of(p)
401
      end
402
      Issue.update_versions_from_hierarchy_change(self)
406
      nested_set_rebuild(p)
403 407
      true
404 408
    else
405 409
      # Can not move to the given target
......
660 664
    p = Project.find(:first, :order => 'created_on DESC')
661 665
    p.nil? ? nil : p.identifier.to_s.succ
662 666
  end
667
  
668
  def save
669
    changed = name_changed?
670
    ret_val = super
671
    if changed
672
      nested_set_rebuild(parent)
673
    end
674
    ret_val
675
  end
663 676

  
664 677
  # Copies and saves the Project instance based on the +project+.
665 678
  # Duplicates the source project's:
test/unit/project_test.rb Mon Jul 30 01:32:31 2012 +0000 → test/unit/project_test.rb Tue Jul 31 00:42:18 2012 -0400
300 300
    assert_equal 4, parent.children.size
301 301
    assert_equal parent.children.all.sort_by(&:name), parent.children.all
302 302
  end
303
  
304
  # See issue #11508, updating a project does not reorder sibilings alphabetically.
305
  def test_rebuild_should_sort_children_alphabetically_on_update
306
    ProjectCustomField.delete_all
307
    
308
    # Create 4 projects under a parent project.
309
    parent = Project.create!(:name => 'Parent', :identifier => 'parent')
310
    Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
311
    Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
312
    Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
313
    renamed_project = Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
303 314

  
315
    # Rebuild them all.
316
    Project.update_all("lft = NULL, rgt = NULL")
317
    Project.rebuild!
304 318

  
319
    # Confirm that everything is as expected.
320
    all_projects = Project.find(:all, :order => 'lft')
321
    assert_equal 'Project D', all_projects.last.name
322

  
323
    # Load and modify project-a, which causes defect #11508
324
    project = Project.find_by_id(renamed_project.id)
325
    project.name = 'Project E'
326
    assert_equal true, project.save
327

  
328
    # Reload all projects and verify that "Project E" is last.
329
    all_projects = Project.find(:all, :order => 'lft')
330
    assert_equal 'Project E', all_projects.last.name
331
  end
332
  
333
  
305 334
  def test_set_parent_should_update_issue_fixed_version_associations_when_a_fixed_version_is_moved_out_of_the_hierarchy
306 335
    # Parent issue with a hierarchy project's fixed version
307 336
    parent_issue = Issue.find(1)
(4-4/5)