]> source.dussan.org Git - redmine.git/commitdiff
Fixed: subtasks are deleted (not destroyed) when destroying parent issue (#7385).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 22 Jan 2011 11:46:15 +0000 (11:46 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 22 Jan 2011 11:46:15 +0000 (11:46 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4735 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue.rb
test/unit/issue_nested_set_test.rb
vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb

index 4892a71bb80ba6f9af3f4d46e6a8987c8f141619..40b64f3e70ff366654f399bbdab50547216f3e49 100644 (file)
@@ -34,7 +34,7 @@ class Issue < ActiveRecord::Base
   has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
   has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
   
-  acts_as_nested_set :scope => 'root_id'
+  acts_as_nested_set :scope => 'root_id', :dependent => :destroy
   acts_as_attachable :after_remove => :attachment_removed
   acts_as_customizable
   acts_as_watchable
@@ -89,7 +89,6 @@ class Issue < ActiveRecord::Base
   before_create :default_assign
   before_save :close_duplicates, :update_done_ratio_from_issue_status
   after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal
-  after_destroy :destroy_children
   after_destroy :update_parent_attributes
   
   # Returns true if usr or current user is allowed to view the issue
@@ -758,14 +757,6 @@ class Issue < ActiveRecord::Base
     end
   end
   
-  def destroy_children
-    unless leaf?
-      children.each do |child|
-        child.destroy
-      end
-    end
-  end
-  
   # Update issues so their versions are not pointing to a
   # fixed_version that is not shared with the issue's project
   def self.update_versions(conditions=nil)
index df9adbe38b6aaee162c418fadc6c2490ee79db76..cf61cfdea8caf19d1792abd9dfa9c4294dcab63e 100644 (file)
@@ -202,7 +202,19 @@ class IssueNestedSetTest < ActiveSupport::TestCase
     issue2 = create_issue!
     issue3 = create_issue!(:parent_issue_id => issue2.id)
     issue4 = create_issue!(:parent_issue_id => issue1.id)
-    issue2.reload.destroy
+    
+    issue3.init_journal(User.find(2))
+    issue3.subject = 'child with journal'
+    issue3.save!
+    
+    assert_difference 'Issue.count', -2 do
+      assert_difference 'Journal.count', -1 do
+        assert_difference 'JournalDetail.count', -1 do
+          Issue.find(issue2.id).destroy
+        end
+      end
+    end
+    
     issue1.reload
     issue4.reload
     assert !Issue.exists?(issue2.id)
@@ -211,6 +223,26 @@ class IssueNestedSetTest < ActiveSupport::TestCase
     assert_equal [issue1.id, 2, 3], [issue4.root_id, issue4.lft, issue4.rgt]
   end
   
+  def test_destroy_child_issue_with_children
+    root = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'root')
+    child = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => root.id)
+    leaf = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'leaf', :parent_issue_id => child.id)
+    leaf.init_journal(User.find(2))
+    leaf.subject = 'leaf with journal'
+    leaf.save!
+    
+    assert_difference 'Issue.count', -2 do
+      assert_difference 'Journal.count', -1 do
+        assert_difference 'JournalDetail.count', -1 do
+          Issue.find(child.id).destroy
+        end
+      end
+    end
+    
+    root = Issue.find(root.id)
+    assert root.leaf?, "Root issue is not a leaf (lft: #{root.lft}, rgt: #{root.rgt})"
+  end
+  
   def test_parent_priority_should_be_the_highest_child_priority
     parent = create_issue!(:priority => IssuePriority.find_by_name('Normal'))
     # Create children
index 0229a8e5caa30353a27cd8e49d4f3c4579a65703..fc5278d693101ec9d36f98d1dcdccd19e5b2cc4f 100644 (file)
@@ -444,17 +444,19 @@ module CollectiveIdea #:nodoc:
         # Prunes a branch off of the tree, shifting all of the elements on the right
         # back to the left so the counts still work.
         def prune_from_tree
-          return if right.nil? || left.nil?
-          diff = right - left + 1
+          return if right.nil? || left.nil? || !self.class.exists?(id)
 
           delete_method = acts_as_nested_set_options[:dependent] == :destroy ?
             :destroy_all : :delete_all
 
           self.class.base_class.transaction do
+            reload_nested_set
             nested_set_scope.send(delete_method,
               ["#{quoted_left_column_name} > ? AND #{quoted_right_column_name} < ?",
                 left, right]
             )
+            reload_nested_set
+            diff = right - left + 1
             nested_set_scope.update_all(
               ["#{quoted_left_column_name} = (#{quoted_left_column_name} - ?)", diff],
               ["#{quoted_left_column_name} >= ?", right]