Commit 1394013f authored by jplang's avatar jplang

Allow forward reference to parent when importing issues (#22701).

git-svn-id: https://svn.redmine.org/redmine/trunk@16241 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent d2a6ae19
......@@ -138,6 +138,24 @@ class Import < ActiveRecord::Base
settings['mapping'] || {}
end
# Adds a callback that will be called after the item at given position is imported
def add_callback(position, name, *args)
settings['callbacks'] ||= {}
settings['callbacks'][position.to_i] ||= []
settings['callbacks'][position.to_i] << [name, args]
save!
end
# Executes the callbacks for the given object
def do_callbacks(position, object)
if callbacks = (settings['callbacks'] || {}).delete(position)
callbacks.each do |name, args|
send "#{name}_callback", object, *args
end
save!
end
end
# Imports items and returns the position of the last processed item
def run(options={})
max_items = options[:max_items]
......@@ -157,7 +175,7 @@ class Import < ActiveRecord::Base
item = items.build
item.position = position
if object = build_object(row)
if object = build_object(row, item)
if object.save
item.obj_id = object.id
else
......@@ -167,6 +185,8 @@ class Import < ActiveRecord::Base
item.save!
imported += 1
do_callbacks(item.position, object)
end
current = position
end
......
......@@ -74,7 +74,7 @@ class IssueImport < Import
private
def build_object(row)
def build_object(row, item)
issue = Issue.new
issue.author = user
issue.notify = false
......@@ -139,11 +139,15 @@ class IssueImport < Import
end
if parent_issue_id = row_value(row, 'parent_issue_id')
if parent_issue_id =~ /\A(#)?(\d+)\z/
parent_issue_id = $2
parent_issue_id = $2.to_i
if $1
attributes['parent_issue_id'] = parent_issue_id
elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
attributes['parent_issue_id'] = issue_id
else
if parent_issue_id > item.position
add_callback(parent_issue_id, 'set_as_parent', item.position)
elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
attributes['parent_issue_id'] = issue_id
end
end
else
attributes['parent_issue_id'] = parent_issue_id
......@@ -183,4 +187,17 @@ class IssueImport < Import
issue
end
# Callback that sets issue as the parent of a previously imported issue
def set_as_parent_callback(issue, child_position)
child_id = items.where(:position => child_position).first.try(:obj_id)
return unless child_id
child = Issue.find_by_id(child_id)
return unless child
child.parent_issue_id = issue.id
child.save!
issue.reload
end
end
row;tracker;subject;parent
1;bug;Root;
2;bug;Child 1;1
3;bug;Grand-child;4
4;bug;Child 2;1
......@@ -115,6 +115,19 @@ class IssueImportTest < ActiveSupport::TestCase
assert_equal 2, issues[2].parent_id
end
def test_backward_and_forward_reference_to_parent_should_work
import = generate_import('import_subtasks.csv')
import.settings = {
'separator' => ";", 'wrapper' => '"', 'encoding' => "UTF-8",
'mapping' => {'project_id' => '1', 'tracker' => '1', 'subject' => '2', 'parent_issue_id' => '3'}
}
import.save!
root, child1, grandchild, child2 = new_records(Issue, 4) { import.run }
assert_equal root, child1.parent
assert_equal child2, grandchild.parent
end
def test_assignee_should_be_set
import = generate_import_with_mapping
import.mapping.merge!('assigned_to' => '11')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment