diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2017-01-21 09:08:40 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2017-01-21 09:08:40 +0000 |
commit | 9c645719ee96b182a2fbe3f77129c1fdb4428791 (patch) | |
tree | 9dd09039233ab4ce5fe15c9161647cac6820388c /app/models | |
parent | 83bc9d2315f55f9e9982654b874edc86353d631d (diff) | |
download | redmine-9c645719ee96b182a2fbe3f77129c1fdb4428791.tar.gz redmine-9c645719ee96b182a2fbe3f77129c1fdb4428791.zip |
Allow forward reference to parent when importing issues (#22701).
git-svn-id: http://svn.redmine.org/redmine/trunk@16241 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/import.rb | 22 | ||||
-rw-r--r-- | app/models/issue_import.rb | 25 |
2 files changed, 42 insertions, 5 deletions
diff --git a/app/models/import.rb b/app/models/import.rb index 22b90b6ac..b3aa914af 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -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 diff --git a/app/models/issue_import.rb b/app/models/issue_import.rb index 4ecd4b517..83a104b37 100644 --- a/app/models/issue_import.rb +++ b/app/models/issue_import.rb @@ -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 |