Skip to content

Commit

Permalink
File import controller enhancements (#714)
Browse files Browse the repository at this point in the history
* starting to create the group metadata file import controller

* adding translations

* trying to move action into the new file import concern
  • Loading branch information
ksierks authored Aug 22, 2024
1 parent 69c7379 commit 28060cc
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 18 deletions.
31 changes: 31 additions & 0 deletions app/controllers/concerns/file_import_actions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# Common file import actions
module FileImportActions
extend ActiveSupport::Concern

included do
before_action proc { namespace }
end

def create
authorize! @namespace, to: :update_sample_metadata?
@imported_metadata = ::Samples::Metadata::FileImportService.new(@namespace, current_user,
file_import_params).execute
if @namespace.errors.empty?
render status: :ok, locals: { type: :success, message: t('.success') }
elsif @namespace.errors.include?(:sample)
errors = @namespace.errors.messages_for(:sample)
render status: :partial_content, locals: { type: :alert, message: t('.error'), errors: }
else
error = @namespace.errors.full_messages_for(:base).first
render status: :unprocessable_entity, locals: { type: :danger, message: error }
end
end

private

def file_import_params
params.require(:file_import).permit(:file, :sample_id_column, :ignore_empty_values)
end
end
24 changes: 24 additions & 0 deletions app/controllers/groups/samples/metadata/file_imports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Groups
module Samples
module Metadata
# Controller actions for Group Samples Metadata File Import Controller
class FileImportsController < Groups::ApplicationController
include FileImportActions

respond_to :turbo_stream

private

def namespace
@namespace = group
end

def group
@group = Group.find_by_full_path(params[:group_id]) # rubocop:disable Rails/DynamicFindBy
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,14 @@ module Samples
module Metadata
# Controller actions for Project Samples Metadata File Import Controller
class FileImportsController < Projects::ApplicationController
respond_to :turbo_stream
include FileImportActions

def create # rubocop:disable Metrics/AbcSize
@namespace = @project.namespace
authorize! @namespace, to: :update_sample_metadata?
@imported_metadata = ::Samples::Metadata::FileImportService.new(@namespace, current_user,
file_import_params).execute
if @namespace.errors.empty?
render status: :ok, locals: { type: :success, message: t('.success') }
elsif @namespace.errors.include?(:sample)
errors = @namespace.errors.messages_for(:sample)
render status: :partial_content, locals: { type: :alert, message: t('.error'), errors: }
else
error = @namespace.errors.full_messages_for(:base).first
render status: :unprocessable_entity, locals: { type: :danger, message: error }
end
end
respond_to :turbo_stream

private

def file_import_params
params.require(:file_import).permit(:file, :sample_id_column, :ignore_empty_values)
def namespace
@namespace = @project.namespace
end
end
end
Expand Down
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,11 @@ en:
selected: Selected
no_samples: No Samples
no_associated_samples: There are no samples associated with this group.
metadata:
file_imports:
create:
success: Sample metadata was successfully imported.
error: "Metadata was not successfully imported for the following samples:"
models:
sample:
analysis: Analysis
Expand Down
5 changes: 5 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,11 @@ fr:
selected: Selected
no_samples: No Samples
no_associated_samples: There are no samples associated with this group.
metadata:
file_imports:
create:
success: Sample metadata was successfully imported.
error: "Metadata was not successfully imported for the following samples:"
models:
sample:
analysis: Analysis
Expand Down
5 changes: 5 additions & 0 deletions config/routes/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

resources :group_links, only: %i[create destroy update index new]
resources :samples, only: %i[index] do
scope module: :samples, as: :samples do
collection do
resource :file_import, module: :metadata, only: %i[create new]
end
end
collection do
get :select
post :search
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# frozen_string_literal: true

require 'test_helper'

module Groups
module Samples
module Metadata
class FileImportControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in users(:john_doe)
@group = groups(:group_one)
@csv = fixture_file_upload('test/fixtures/files/metadata/valid.csv')
end

test 'import sample metadata with permission' do
post group_samples_file_import_path(@group, format: :turbo_stream),
params: {
file_import: {
file: @csv,
sample_id_column: 'sample_name'
}
}

assert_response :success
end

test 'import sample metadata without permission' do
login_as users(:micha_doe)

post group_samples_file_import_path(@group, format: :turbo_stream),
params: {
file_import: {
file: @csv,
sample_id_column: 'sample_name'
}
}

assert_response :unauthorized
end

test 'import sample metadata with no file' do
post group_samples_file_import_path(@group, format: :turbo_stream),
params: {
file_import: { sample_id_column: 'sample_name' }
}
assert_response :unprocessable_entity
end

test 'import sample metadata with no sample_id_column' do
csv = File.new('test/fixtures/files/metadata/missing_sample_id_column.csv', 'r')
post group_samples_file_import_path(@group, format: :turbo_stream),
params: {
file_import: { file: csv, sample_id_column: 'sample_name' }
}
assert_response :unprocessable_entity
end

test 'import sample metadata with duplicate column names' do
csv = File.new('test/fixtures/files/metadata/duplicate_headers.csv', 'r')
post group_samples_file_import_path(@group, format: :turbo_stream),
params: {
file_import: { file: csv, sample_id_column: 'sample_name' }
}
assert_response :unprocessable_entity
end

test 'import sample metadata with no metadata columns' do
csv = File.new('test/fixtures/files/metadata/missing_metadata_columns.csv', 'r')
post group_samples_file_import_path(@group, format: :turbo_stream),
params: {
file_import: { file: csv, sample_id_column: 'sample_name' }
}
assert_response :unprocessable_entity
end

test 'import sample metadata with no metadata rows' do
csv = File.new('test/fixtures/files/metadata/missing_metadata_rows.csv', 'r')
post group_samples_file_import_path(@group, format: :turbo_stream),
params: {
file_import: { file: csv, sample_id_column: 'sample_name' }
}
assert_response :unprocessable_entity
end

test 'import sample metadata with invalid file' do
other = fixture_file_upload('test/fixtures/files/metadata/invalid.txt')
post group_samples_file_import_path(@group, format: :turbo_stream),
params: {
file_import: {
file: other,
sample_id_column: 'sample_name'
}
}

assert_response :unprocessable_entity
end

test 'import sample metadata with a sample that does not belong to group' do
csv = fixture_file_upload('test/fixtures/files/metadata/mixed_project_samples.csv')
post group_samples_file_import_path(@group, format: :turbo_stream),
params: {
file_import: {
file: csv,
sample_id_column: 'sample_name'
}
}

assert_response :partial_content
end
end
end
end
end

0 comments on commit 28060cc

Please sign in to comment.