Class: SampleManifestExcel::Upload::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/sample_manifest_excel/sample_manifest_excel/upload/base.rb

Overview

An upload will: *Create a Data object based on the file. *Extract the columns based on the headings in the spreadsheet *Find the sanger sample id column *Create some Rows *Retrieve the sample manifest *Create a processor based on the sample manifest The Upload is only valid if the file, columns, sample manifest and processor are valid.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base


34
35
36
37
38
39
40
41
42
43
44
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 34

def initialize(attributes = {})
  super
  @data = Upload::Data.new(file, start_row)
  @columns = column_list.extract(data.header_row.reject(&:blank?) || [])
  @sanger_sample_id_column = columns.find_by(:name, :sanger_sample_id)
  @cache = Cache.new(self)
  @rows = Upload::Rows.new(data, columns, @cache)
  @sample_manifest = derive_sample_manifest
  @override = override || false
  @processor = create_processor
end

Instance Attribute Details

#column_listObject

Returns the value of attribute column_list


19
20
21
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 19

def column_list
  @column_list
end

#columnsObject (readonly)

Returns the value of attribute columns


21
22
23
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 21

def columns
  @columns
end

#dataObject (readonly)

Returns the value of attribute data


21
22
23
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 21

def data
  @data
end

#fileObject

Returns the value of attribute file


19
20
21
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 19

def file
  @file
end

#overrideObject

Returns the value of attribute override


19
20
21
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 19

def override
  @override
end

#processorObject (readonly)

Returns the value of attribute processor


21
22
23
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 21

def processor
  @processor
end

#rowsObject (readonly)

Returns the value of attribute rows


21
22
23
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 21

def rows
  @rows
end

#sample_manifestObject (readonly)

Returns the value of attribute sample_manifest


21
22
23
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 21

def sample_manifest
  @sample_manifest
end

#sanger_sample_id_columnObject (readonly)

Returns the value of attribute sanger_sample_id_column


21
22
23
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 21

def sanger_sample_id_column
  @sanger_sample_id_column
end

#spreadsheetObject (readonly)

Returns the value of attribute spreadsheet


21
22
23
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 21

def spreadsheet
  @spreadsheet
end

#start_rowObject

Returns the value of attribute start_row


19
20
21
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 19

def start_row
  @start_row
end

Instance Method Details

#broadcast_sample_manifest_updated_event(user) ⇒ Object


84
85
86
87
88
89
90
91
92
93
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 84

def broadcast_sample_manifest_updated_event(user)
  # Send to event warehouse
  sample_manifest.updated_broadcast_event(user, samples_to_be_broadcasted.map(&:id))
  # Log legacy events: Show on history page, and may be used by reports.
  # We can get rid of these when:
  # - History page is updates with event warehouse viewer
  # - We've confirmed that no external reports use these events
  samples_to_be_broadcasted.each { |sample| sample.handle_update_event(user) }
  labware_to_be_broadcasted.each { |labware| labware.events.updated_using_sample_manifest!(user) }
end

#completeObject


95
96
97
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 95

def complete
  sample_manifest.finished!
end

#data_at(column_name) ⇒ Object


79
80
81
82
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 79

def data_at(column_name)
  required_column = columns.find_by(:name, column_name)
  rows.data_at(required_column.number) if required_column.present?
end

#derive_sample_manifestObject

The sample manifest is retrieved by taking the sample from the first row and retrieving its sample manifest. If it can't be found the upload will fail.


54
55
56
57
58
59
60
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 54

def derive_sample_manifest
  return unless start_row.present? && sanger_sample_id_column.present?

  sanger_sample_id = data.cell(1, sanger_sample_id_column.number)
  SampleManifestAsset.find_by(sanger_sample_id: sanger_sample_id)&.sample_manifest ||
    Sample.find_by(sanger_sample_id: sanger_sample_id)&.sample_manifest
end

#failObject


99
100
101
102
103
104
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 99

def fail
  # If we've failed, do not update the manifest file, trying to do so
  # causes exceptions
  sample_manifest.association(:uploaded_document).reset
  sample_manifest.fail!
end

#inspectObject


46
47
48
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 46

def inspect
  "<#{self.class}: @file=#{file}, @columns=#{columns.inspect}, @start_row=#{start_row}, @sanger_sample_id_column=#{sanger_sample_id_column}, @data=#{data.inspect}>"
end

#process(tag_group) ⇒ Object

An upload can only be processed if the upload is valid. Processing involves updating the sample manifest and all of its associated samples.


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 65

def process(tag_group)
  ActiveRecord::Base.transaction do
    sample_manifest.last_errors = nil
    sample_manifest.start!
    @cache.populate!
    processor.run(tag_group)
    return true if processed?

    # One of out post processing checks failed, something went wrong, so we
    # roll everything back
    raise ActiveRecord::Rollback
  end
end