Module: Tasks::PlateTransferHandler

Included in:
WorkflowsController
Defined in:
app/models/tasks/plate_transfer_handler.rb

Overview

rubocop:todo Style/Documentation

Defined Under Namespace

Classes: InvalidBatch

Instance Method Summary collapse

Instance Method Details

#do_plate_transfer_task(_task, _params) ⇒ Object


65
66
67
68
69
# File 'app/models/tasks/plate_transfer_handler.rb', line 65

def do_plate_transfer_task(_task, _params)
  return if target_plate.state == 'passed'

  StateChange.create(target: target_plate, target_state: 'passed', user: current_user)
end

#find_or_create_target(task) ⇒ Object

rubocop:todo Metrics/MethodLength

Raises:


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/tasks/plate_transfer_handler.rb', line 15

def find_or_create_target(task) # rubocop:todo Metrics/AbcSize
  return target_plate if target_plate.present?

  # We only eager load the request stuff if we actually need it.
  batch_requests = @batch.requests.includes(includes_for_plate_creation)
  source_wells = batch_requests.map(&:asset)
  raise InvalidBatch if unsuitable_wells?(source_wells)

  task
    .purpose
    .create!
    .tap do |target|
      well_map = target.wells.index_by { |well| well.map_id }

      batch_requests.each do |outer_request|
        source = outer_request.asset
        TransferRequest.create!(
          asset: source,
          target_asset: well_map[source.map_id],
          submission_id: outer_request.submission_id
        )
        TransferRequest.create!(
          asset: well_map[source.map_id],
          target_asset: outer_request.target_asset,
          submission_id: outer_request.submission_id
        )
      end
    end
end

#includes_for_plate_creationObject


10
11
12
# File 'app/models/tasks/plate_transfer_handler.rb', line 10

def includes_for_plate_creation
  [{ asset: [:map, { plate: %i[plate_purpose barcodes] }, :aliquots] }, { target_asset: [] }]
end

#render_plate_transfer_task(task, _params) ⇒ Object


6
7
8
# File 'app/models/tasks/plate_transfer_handler.rb', line 6

def render_plate_transfer_task(task, _params)
  ActiveRecord::Base.transaction { @target = find_or_create_target(task) }
end

#target_plateObject

rubocop:enable Metrics/MethodLength


47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/tasks/plate_transfer_handler.rb', line 47

def target_plate
  transfer =
    TransferRequest
      .for_request(@batch.requests.first)
      .where(submission_id: @batch.requests.first.submission_id)
      .includes(target_asset: :plate)
      .first
  return nil if transfer.blank?

  transfer.target_asset.plate
end