diff --git a/app/assets/javascripts/repositories/forms/repository_item_edit.js b/app/assets/javascripts/repositories/forms/repository_item_edit.js index 1cbd8b08a..0a36e2d9e 100644 --- a/app/assets/javascripts/repositories/forms/repository_item_edit.js +++ b/app/assets/javascripts/repositories/forms/repository_item_edit.js @@ -115,6 +115,7 @@ RepositoryItemEditForm.prototype.parseToFormObject = function(tableID, selectedRecord) { var formData = this.formData; var formDataObj = new FormData(); + var removeFileColumns = []; formDataObj.append('request_url', $(tableID).data('current-uri')); formDataObj.append('repository_row_id', $(selectedRecord).attr('id')); @@ -125,18 +126,25 @@ } else { var colId = element.replace('colId-', ''); var $el = $('#' + element); + // don't save anything if element is not visible + if($el.length == 0) { + return true; + } if($el.attr('type') === 'file') { - // don't save anything if element is deleted + // handle deleting of element if($el.attr('remove') === "true") { - return true; + removeFileColumns.push(colId); + formDataObj.append('repository_cells[' + colId + ']', null); + } else { + formDataObj.append('repository_cells[' + colId + ']', + getFileValue($el)); } - formDataObj.append('repository_cells[' + colId + ']', - getFileValue($el)); - } else if(value.length > 0) { + } else if(value.length >= 0) { formDataObj.append('repository_cells[' + colId + ']', value); } } }); + formDataObj.append('remove_file_columns', JSON.stringify(removeFileColumns)); return formDataObj; } /** diff --git a/app/controllers/repository_rows_controller.rb b/app/controllers/repository_rows_controller.rb index b7feb3f0e..942634b2f 100644 --- a/app/controllers/repository_rows_controller.rb +++ b/app/controllers/repository_rows_controller.rb @@ -134,7 +134,7 @@ class RepositoryRowsController < ApplicationController existing.delete end elsif existing.value_type == 'RepositoryAssetValue' - next if value.blank? + existing.value.destroy && next if remove_file_columns_params.include?(key) if existing.value.asset.update(file: value) existing.value.asset.created_by = current_user existing.value.asset.last_modified_by = current_user @@ -145,6 +145,7 @@ class RepositoryRowsController < ApplicationController } end else + existing.value.destroy && next if value == '' existing.value.data = value if existing.value.save record_annotation_notification(@record, existing) @@ -156,17 +157,12 @@ class RepositoryRowsController < ApplicationController end end else + next if value == '' # Looks like it is a new cell, so we need to create new value, cell # will be created automatically next if create_cell_value(@record, key, value, errors).nil? end end - # Clean up empty cells, not present in updated record - @record.repository_cells.each do |cell| - next if cell.value_type == 'RepositoryListValue' - cell.value.destroy unless cell_params - .key?(cell.repository_column_id.to_s) - end else @record.repository_cells.each { |c| c.value.destroy } end @@ -369,6 +365,10 @@ class RepositoryRowsController < ApplicationController params.permit(repository_cells: {}).to_h[:repository_cells] end + def remove_file_columns_params + JSON.parse(params.fetch(:remove_file_columns) { '[]' }) + end + def selected_params params.permit(selected_rows: []).to_h[:selected_rows] end