commit 264176c2072d23ecf10d1b611df36f6d5f98779b Author: Marius BALTEANU Date: Mon Oct 7 12:28:36 2019 +0300 Auto map fields for import diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb index 633cc232c..f549ee2de 100644 --- a/app/controllers/imports_controller.rb +++ b/app/controllers/imports_controller.rb @@ -66,6 +66,8 @@ class ImportsController < ApplicationController def mapping @custom_fields = @import.mappable_custom_fields + auto_map_fields if @import.mapping.empty? + if request.post? respond_to do |format| format.html { @@ -159,4 +161,26 @@ class ImportsController < ApplicationController type && type < Import ? type : nil end end + + def auto_map_fields + @import.settings['mapping'] = {} + + headers = @import.headers.map { |string| string.parameterize.underscore } + mappings = @import.mapping + + # Try to auto map core fields + import_type::AUTO_MAPPABLE_FIELDS.each do |field| + if !mappings.include?(field) && headers.include?(field) + @import.mapping[field] = headers.index(field) + end + end + + # Try to auto map custom fields + @custom_fields.each do |field| + field_name = field.name.parameterize.underscore + if headers.include?(field_name) + @import.mapping["cf_#{field.id}"] = headers.index(field_name) + end + end + end end diff --git a/app/models/import.rb b/app/models/import.rb index 61b3553f3..969f52014 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -38,6 +38,8 @@ class Import < ActiveRecord::Base '%d-%m-%Y' ] + AUTO_MAPPABLE_FIELDS = [] + def self.menu_item nil end diff --git a/app/models/issue_import.rb b/app/models/issue_import.rb index 206c0bfd5..953b84e5e 100644 --- a/app/models/issue_import.rb +++ b/app/models/issue_import.rb @@ -18,6 +18,25 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class IssueImport < Import + + AUTO_MAPPABLE_FIELDS = [ + 'tracker', + 'status', + 'unique_id', + 'subject', + 'description', + 'priority', + 'category', + 'assigned_to', + 'fixed_version', + 'is_private', + 'parent_issue_id', + 'start_date', + 'due_date', + 'estimated_hours', + 'done_ratio' + ] + def self.menu_item :issues end