Class: SampleManifest::Uploader

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/sample_manifest/uploader.rb

Overview

Class SampleManifest::Uploader provides an interface for uploading sample manifests from a controller

Author:

  • Genome Research Ltd.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, configuration, user, override) ⇒ Uploader

Returns a new instance of Uploader.



21
22
23
24
25
26
27
28
29
# File 'app/models/sample_manifest/uploader.rb', line 21

def initialize(file, configuration, user, override)
  @file = file
  @configuration = configuration || SequencescapeExcel::NullObjects::NullConfiguration.new
  @user = user
  @override = override
  @tag_group = create_tag_group
  @upload =
    SampleManifestExcel::Upload::Base.new(file: file, column_list: self.configuration.columns.all, override: override)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



12
13
14
# File 'app/models/sample_manifest/uploader.rb', line 12

def configuration
  @configuration
end

#fileObject (readonly)

Returns the value of attribute file.



12
13
14
# File 'app/models/sample_manifest/uploader.rb', line 12

def file
  @file
end

#overrideObject (readonly)

Returns the value of attribute override.



12
13
14
# File 'app/models/sample_manifest/uploader.rb', line 12

def override
  @override
end

#tag_groupObject (readonly)

Returns the value of attribute tag_group.



12
13
14
# File 'app/models/sample_manifest/uploader.rb', line 12

def tag_group
  @tag_group
end

#uploadObject (readonly)

Returns the value of attribute upload.



12
13
14
# File 'app/models/sample_manifest/uploader.rb', line 12

def upload
  @upload
end

#userObject (readonly)

Returns the value of attribute user.



12
13
14
# File 'app/models/sample_manifest/uploader.rb', line 12

def user
  @user
end

Instance Method Details

#run!Object

rubocop:disable Metrics/MethodLength



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/sample_manifest/uploader.rb', line 31

def run! # rubocop:disable Metrics/MethodLength
  # Validation outside the transaction because we want to return the errors
  return false unless valid?

  # Start the upload so that we can prevent concurrent uploads
  upload.sample_manifest.start!

  # Rails 6.1 doesn't allow return value from transaction block
  success =
    ActiveRecord::Base.transaction do
      raise ActiveRecord::Rollback unless process_upload_and_callbacks
      true
    end

  # Return from the function if the transaction succeeded
  return true if success

  # Else, return false and extract the errors
  extract_errors
  upload.fail
  false
end