diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index d5d53c28f..60319a21f 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -232,15 +232,19 @@ class RepositoriesController < ApplicationController def import_records import_records = repostiory_import_actions status = import_records.import! + respond_to do |format| + format.json do + if status[:status] == :ok + flash[:success] = t('repositories.import_records.success_flash', + number_of_rows: status[:nr_of_added]) + render json: {}, status: :ok + else + flash[:alert] = t('repositories.import_records.error_flash', + message: status[:errors]) + render json: {}, status: :unprocessable_entity + end + end - if status[:status] == :ok - flash[:success] = t('repositories.import_records.success_flash', - number_of_rows: status[:nr_of_added]) - head :ok - else - flash[:alert] = t('repositories.import_records.error_flash', - message: status[:errors]) - head :unprocessable_entity end end diff --git a/app/models/repository.rb b/app/models/repository.rb index 85a5763ec..bc8b8264c 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -29,7 +29,7 @@ class Repository < ActiveRecord::Base def available_repository_fields fields = {} # First and foremost add sample name - fields['-1'] = I18n.t('samples.table.sample_name') + fields['-1'] = I18n.t('repositories.default_column') # Add all other custom columns repository_columns.order(:created_at).each do |rc| fields[rc.id] = rc.name @@ -69,10 +69,7 @@ class Repository < ActiveRecord::Base errors = [] custom_fields = [] name_index = -1 - total_nr = 0 nr_of_added = 0 - header = sheet.row(1) - generate_new_columns(header) mappings.each.with_index do |(k, v), i| if v == '-1' @@ -80,8 +77,7 @@ class Repository < ActiveRecord::Base custom_fields << nil name_index = i else - cf = repository_columns.find_by_name(header[i]) - + cf = repository_columns.find_by_id(v) custom_fields << cf end end @@ -89,19 +85,15 @@ class Repository < ActiveRecord::Base # Now we can iterate through sample data and save stuff into db (2..sheet.last_row).each do |i| error = [] - total_nr += 1 + nr_of_added += 1 record_row = RepositoryRow.new(name: sheet.row(i)[name_index], repository: self, created_by: user, last_modified_by: user) - if record_row.save sheet.row(i).each.with_index do |value, index| - # We need to have sample saved before messing with custom fields (they - # need sample id) if custom_fields[index] - nr_of_added += 1 # we're working with CustomField rep_column = RepositoryTextValue.new( data: value, @@ -125,30 +117,20 @@ class Repository < ActiveRecord::Base end if errors.count > 0 - return { - status: :error, - errors: errors, - nr_of_added: nr_of_added, - total_nr: total_nr - } - else - return { - status: :ok, - nr_of_added: nr_of_added, - total_nr: total_nr - } + return { status: :error, errors: errors, nr_of_added: nr_of_added } end + { status: :ok, nr_of_added: nr_of_added } end private - def generate_new_columns(header) - rep_columns_names = self.repository_columns.pluck(:name).push('Name') - header.each do |cname| - next if rep_columns_names.include? cname - RepositoryColumn.create(repository: self, name: cname, data_type: 0) - end - end + # def generate_new_columns(header) + # rep_columns_names = self.repository_columns.pluck(:name).push('Name') + # header.each do |cname| + # next if rep_columns_names.include? cname + # RepositoryColumn.create(repository: self, name: cname, data_type: 0) + # end + # end def generate_file(filename, file_path) case File.extname(filename) diff --git a/app/services/import_repository/import_records.rb b/app/services/import_repository/import_records.rb index bb35a1dcd..f50558838 100644 --- a/app/services/import_repository/import_records.rb +++ b/app/services/import_repository/import_records.rb @@ -9,7 +9,6 @@ module ImportRepository end def import! - status = run_import_actions @temp_file.destroy status diff --git a/app/views/repositories/_parse_records_modal.html.erb b/app/views/repositories/_parse_records_modal.html.erb index 70938e19c..223a36c66 100644 --- a/app/views/repositories/_parse_records_modal.html.erb +++ b/app/views/repositories/_parse_records_modal.html.erb @@ -6,7 +6,7 @@ <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 01bc0f7fd..3bc87287e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -892,11 +892,11 @@ en: success_flash: "%{number_of_rows} new record(s) successfully imported." error_flash: "Something went wrong: %{message}" error_message: - temp_file_not_found: 'This file could not be found. Your session might expire.' - session_expired: 'Your session expired. Please try again.' - no_data_to_parse: 'There's nothing to be parsed.' - no_column_name: 'Name column is required!' - duplicated_values: 'Two or more columns have the same mapping.' + temp_file_not_found: "This file could not be found. Your session might expire." + session_expired: "Your session expired. Please try again." + no_data_to_parse: "There's nothing to be parsed." + no_column_name: "Name column is required!" + duplicated_values: "Two or more columns have the same mapping." edit_record: "Edit" delete_record: "Delete" save_record: "Save" @@ -922,6 +922,8 @@ en: alert_line_1: "you will lose information in this column for %{nr} records;" alert_line_2: "the column will be deleted for all team members." delete: "Delete column" + modal_parse: + title: 'Import records' modal_import: title: 'Import records' notice: 'You may upload .csv file (comma separated) or tab separated file (.txt or .tdv) or Excel file (.xls, .xlsx). First row should include header names, followed by rows with sample data.' @@ -944,7 +946,8 @@ en: unassigned_records_flash: "Successfully unassigned record(s) %{records} from task" no_records_assigned_flash: "No records were assigned to task" no_records_unassigned_flash: "No records were unassigned from task" - + default_column: 'Name' + samples: columns: "Columns" columns_visibility: "Toggle visibility"