]> source.dussan.org Git - redmine.git/commitdiff
Recalculate inherited attributes on parents when a child is moved under a new parent...
authorEric Davis <edavis@littlestreamsoftware.com>
Wed, 30 Jun 2010 02:45:34 +0000 (02:45 +0000)
committerEric Davis <edavis@littlestreamsoftware.com>
Wed, 30 Jun 2010 02:45:34 +0000 (02:45 +0000)
Contributed by Jean-Baptiste Barth.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3821 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue.rb
test/unit/issue_nested_set_test.rb

index 2d002183533a415c6ac1e114f9ef7cb63251ec72..7d0682df11565108e4212d0ea56596e251989c58 100644 (file)
@@ -629,6 +629,7 @@ class Issue < ActiveRecord::Base
       end
       reload
     elsif parent_issue_id != parent_id
+      former_parent_id = parent_id
       # moving an existing issue
       if @parent_issue && @parent_issue.root_id == root_id
         # inside the same tree
@@ -658,12 +659,18 @@ class Issue < ActiveRecord::Base
           relation.destroy unless relation.valid?
         end
       end
+      # update former parent
+      recalculate_attributes_for(former_parent_id) if former_parent_id
     end
     remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
   end
   
   def update_parent_attributes
-    if parent_id && p = Issue.find_by_id(parent_id)
+    recalculate_attributes_for(parent_id) if parent_id
+  end
+
+  def recalculate_attributes_for(issue_id)
+    if issue_id && p = Issue.find_by_id(issue_id)
       # priority = highest priority of children
       if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :include => :priority)
         p.priority = IssuePriority.find_by_position(priority_position)
index 7138e4527d90b40821c8e0fbbc27c22eddd60394..cda8c4e587920395ceae4135b6885a67adb583cd 100644 (file)
@@ -273,6 +273,15 @@ class IssueNestedSetTest < ActiveSupport::TestCase
     assert_equal 12, parent.reload.estimated_hours
   end
 
+  def test_move_parent_updates_old_parent_attributes
+    first_parent = create_issue!
+    second_parent = create_issue!
+    child = create_issue!(:estimated_hours => 5, :parent_issue_id => first_parent.id)
+    assert_equal 5, first_parent.reload.estimated_hours
+    child.update_attributes(:estimated_hours => 7, :parent_issue_id => second_parent.id)
+    assert_equal 7, second_parent.reload.estimated_hours
+    assert_nil first_parent.reload.estimated_hours
+  end
 
   def test_reschuling_a_parent_should_reschedule_subtasks
     parent = create_issue!