]> source.dussan.org Git - redmine.git/commitdiff
Fixed: deleting a project with subprojects breaks the project tree (#4701).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 31 Jan 2010 10:39:42 +0000 (10:39 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 31 Jan 2010 10:39:42 +0000 (10:39 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3354 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/project.rb
test/unit/project_nested_set_test.rb [new file with mode: 0644]

index 490c290be8e170534cd4a89aa60fde99369b6e01..b8030d7fd9345c1cc4cb74dff7ff6ca7958adef8 100644 (file)
@@ -51,7 +51,7 @@ class Project < ActiveRecord::Base
                           :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
                           :association_foreign_key => 'custom_field_id'
                           
-  acts_as_nested_set :order => 'name', :dependent => :destroy
+  acts_as_nested_set :order => 'name'
   acts_as_attachable :view_permission => :view_files,
                      :delete_permission => :manage_files
 
@@ -74,7 +74,7 @@ class Project < ActiveRecord::Base
   # reserved words
   validates_exclusion_of :identifier, :in => %w( new )
 
-  before_destroy :delete_all_members
+  before_destroy :delete_all_members, :destroy_children
 
   named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
   named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
@@ -499,6 +499,13 @@ class Project < ActiveRecord::Base
   
   private
   
+  # Destroys children before destroying self
+  def destroy_children
+    children.each do |child|
+      child.destroy
+    end
+  end
+  
   # Copies wiki from +project+
   def copy_wiki(project)
     # Check that the source project has a wiki first
diff --git a/test/unit/project_nested_set_test.rb b/test/unit/project_nested_set_test.rb
new file mode 100644 (file)
index 0000000..6aa09d6
--- /dev/null
@@ -0,0 +1,58 @@
+# Redmine - project management software
+# Copyright (C) 2006-2010  Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+require File.dirname(__FILE__) + '/../test_helper'
+
+class ProjectNestedSetTest < ActiveSupport::TestCase
+  
+  def setup
+    Project.delete_all
+  end
+  
+  def test_destroy_root_and_chldren_should_not_mess_up_the_tree
+    a = Project.create!(:name => 'Project A', :identifier => 'projecta')
+    a1 = Project.create!(:name => 'Project A1', :identifier => 'projecta1')
+    a2 = Project.create!(:name => 'Project A2', :identifier => 'projecta2')
+    a1.set_parent!(a)
+    a2.set_parent!(a)
+    b = Project.create!(:name => 'Project B', :identifier => 'projectb')
+    b1 = Project.create!(:name => 'Project B1', :identifier => 'projectb1')
+    b1.set_parent!(b)
+    
+    a.reload
+    a1.reload
+    a2.reload
+    b.reload
+    b1.reload
+    
+    assert_equal [nil, 1, 6], [a.parent_id, a.lft, a.rgt]
+    assert_equal [a.id, 2, 3], [a1.parent_id, a1.lft, a1.rgt]
+    assert_equal [a.id, 4, 5], [a2.parent_id, a2.lft, a2.rgt]
+    assert_equal [nil, 7, 10], [b.parent_id, b.lft, b.rgt]
+    assert_equal [b.id, 8, 9], [b1.parent_id, b1.lft, b1.rgt]
+    
+    assert_difference 'Project.count', -3 do
+      a.destroy
+    end
+    
+    b.reload
+    b1.reload
+
+    assert_equal [nil, 1, 4], [b.parent_id, b.lft, b.rgt]
+    assert_equal [b.id, 2, 3], [b1.parent_id, b1.lft, b1.rgt]
+  end
+end
\ No newline at end of file