@@ -1105,6 +1105,10 class Issue < ActiveRecord::Base | |||
|
1105 | 1105 | s = arg.to_s.strip.presence |
|
1106 | 1106 | if s && (m = s.match(%r{\A#?(\d+)\z})) && (@parent_issue = Issue.find_by_id(m[1])) |
|
1107 | 1107 | @parent_issue.id |
|
1108 | @invalid_parent_issue_id = nil | |
|
1109 | elsif s.blank? | |
|
1110 | @parent_issue = nil | |
|
1111 | @invalid_parent_issue_id = nil | |
|
1108 | 1112 | else |
|
1109 | 1113 | @parent_issue = nil |
|
1110 | 1114 | @invalid_parent_issue_id = arg |
@@ -1280,38 +1284,43 class Issue < ActiveRecord::Base | |||
|
1280 | 1284 | move_to_child_of(@parent_issue) |
|
1281 | 1285 | end |
|
1282 | 1286 | elsif parent_issue_id != parent_id |
|
1283 | former_parent_id = parent_id | |
|
1284 | # moving an existing issue | |
|
1285 | if @parent_issue && @parent_issue.root_id == root_id | |
|
1286 | # inside the same tree | |
|
1287 | update_nested_set_attributes_on_parent_change | |
|
1288 | end | |
|
1289 | remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue) | |
|
1290 | end | |
|
1291 | ||
|
1292 | # Updates the nested set for when an existing issue is moved | |
|
1293 | def update_nested_set_attributes_on_parent_change | |
|
1294 | former_parent_id = parent_id | |
|
1295 | # moving an existing issue | |
|
1296 | if @parent_issue && @parent_issue.root_id == root_id | |
|
1297 | # inside the same tree | |
|
1298 | move_to_child_of(@parent_issue) | |
|
1299 | else | |
|
1300 | # to another tree | |
|
1301 | unless root? | |
|
1302 | move_to_right_of(root) | |
|
1303 | end | |
|
1304 | old_root_id = root_id | |
|
1305 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id ) | |
|
1306 | target_maxright = nested_set_scope.maximum(right_column_name) || 0 | |
|
1307 | offset = target_maxright + 1 - lft | |
|
1308 | Issue.update_all(["root_id = ?, lft = lft + ?, rgt = rgt + ?", root_id, offset, offset], | |
|
1309 | ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt]) | |
|
1310 | self[left_column_name] = lft + offset | |
|
1311 | self[right_column_name] = rgt + offset | |
|
1312 | if @parent_issue | |
|
1287 | 1313 | move_to_child_of(@parent_issue) |
|
1288 | else | |
|
1289 | # to another tree | |
|
1290 | unless root? | |
|
1291 | move_to_right_of(root) | |
|
1292 | end | |
|
1293 | old_root_id = root_id | |
|
1294 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id ) | |
|
1295 | target_maxright = nested_set_scope.maximum(right_column_name) || 0 | |
|
1296 | offset = target_maxright + 1 - lft | |
|
1297 | Issue.update_all(["root_id = ?, lft = lft + ?, rgt = rgt + ?", root_id, offset, offset], | |
|
1298 | ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt]) | |
|
1299 | self[left_column_name] = lft + offset | |
|
1300 | self[right_column_name] = rgt + offset | |
|
1301 | if @parent_issue | |
|
1302 | move_to_child_of(@parent_issue) | |
|
1303 | end | |
|
1304 | 1314 | end |
|
1305 | # delete invalid relations of all descendants | |
|
1306 | self_and_descendants.each do |issue| | |
|
1307 | issue.relations.each do |relation| | |
|
1308 | relation.destroy unless relation.valid? | |
|
1309 | end | |
|
1315 | end | |
|
1316 | # delete invalid relations of all descendants | |
|
1317 | self_and_descendants.each do |issue| | |
|
1318 | issue.relations.each do |relation| | |
|
1319 | relation.destroy unless relation.valid? | |
|
1310 | 1320 | end |
|
1311 | # update former parent | |
|
1312 | recalculate_attributes_for(former_parent_id) if former_parent_id | |
|
1313 | 1321 | end |
|
1314 | remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue) | |
|
1322 | # update former parent | |
|
1323 | recalculate_attributes_for(former_parent_id) if former_parent_id | |
|
1315 | 1324 | end |
|
1316 | 1325 | |
|
1317 | 1326 | def update_parent_attributes |
@@ -166,6 +166,41 class IssueNestedSetTest < ActiveSupport::TestCase | |||
|
166 | 166 | assert_not_equal [], child.errors[:parent_issue_id] |
|
167 | 167 | end |
|
168 | 168 | |
|
169 | def test_updating_a_root_issue_should_not_trigger_update_nested_set_attributes_on_parent_change | |
|
170 | issue = Issue.find(Issue.generate!.id) | |
|
171 | issue.parent_issue_id = "" | |
|
172 | issue.expects(:update_nested_set_attributes_on_parent_change).never | |
|
173 | issue.save! | |
|
174 | end | |
|
175 | ||
|
176 | def test_updating_a_child_issue_should_not_trigger_update_nested_set_attributes_on_parent_change | |
|
177 | issue = Issue.find(Issue.generate!(:parent_issue_id => 1).id) | |
|
178 | issue.parent_issue_id = "1" | |
|
179 | issue.expects(:update_nested_set_attributes_on_parent_change).never | |
|
180 | issue.save! | |
|
181 | end | |
|
182 | ||
|
183 | def test_moving_a_root_issue_should_trigger_update_nested_set_attributes_on_parent_change | |
|
184 | issue = Issue.find(Issue.generate!.id) | |
|
185 | issue.parent_issue_id = "1" | |
|
186 | issue.expects(:update_nested_set_attributes_on_parent_change).once | |
|
187 | issue.save! | |
|
188 | end | |
|
189 | ||
|
190 | def test_moving_a_child_issue_to_another_parent_should_trigger_update_nested_set_attributes_on_parent_change | |
|
191 | issue = Issue.find(Issue.generate!(:parent_issue_id => 1).id) | |
|
192 | issue.parent_issue_id = "2" | |
|
193 | issue.expects(:update_nested_set_attributes_on_parent_change).once | |
|
194 | issue.save! | |
|
195 | end | |
|
196 | ||
|
197 | def test_moving_a_child_issue_to_root_should_trigger_update_nested_set_attributes_on_parent_change | |
|
198 | issue = Issue.find(Issue.generate!(:parent_issue_id => 1).id) | |
|
199 | issue.parent_issue_id = "" | |
|
200 | issue.expects(:update_nested_set_attributes_on_parent_change).once | |
|
201 | issue.save! | |
|
202 | end | |
|
203 | ||
|
169 | 204 | def test_destroy_should_destroy_children |
|
170 | 205 | issue1 = Issue.generate! |
|
171 | 206 | issue2 = Issue.generate! |
General Comments 0
You need to be logged in to leave comments.
Login now