Class: Plate::Creator

Inherits:
ApplicationRecord show all
Defined in:
app/models/plate/creator.rb

Overview

A plate creator creates a stamp of a parent plate into one or more children A stamp is the complete transfer of content, maintaining the same well locations.

Defined Under Namespace

Classes: ParentPurposeRelationship, PurposeRelationship

Constant Summary collapse

PlateCreationError =

rubocop:todo Metrics/ClassLength

Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Instance Attribute Details

#created_asset_groupObject (readonly)

Returns the value of attribute created_asset_group.



43
44
45
# File 'app/models/plate/creator.rb', line 43

def created_asset_group
  @created_asset_group
end

Instance Method Details

#create_asset_group(created_plates) ⇒ Object

rubocop:todo Metrics/MethodLength



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/plate/creator.rb', line 166

def create_asset_group(created_plates) # rubocop:todo Metrics/MethodLength
  group = nil
  all_wells = created_plates.map { |hash| hash[:destinations].map(&:wells) }.flatten

  study = find_relevant_study(created_plates)
  unless study
    warnings_list << 'Failed to create Asset Group: could not find an appropriate Study to group the plates under.'
    return group
  end

  ActiveRecord::Base.transaction do
    # TO DO: handle exceptions from this?
    group = AssetGroup.create!(study: study, name: asset_group_name)
    group.assets.concat(all_wells)
  end

  group
end

#create_plates_from_tube_racks!(tube_racks, barcode_printer, scanned_user, should_create_asset_group, _creator_parameters = nil) ⇒ Object

rubocop:todo Metrics/MethodLength, Metrics/AbcSize



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/plate/creator.rb', line 101

def create_plates_from_tube_racks!(
  tube_racks,
  barcode_printer,
  scanned_user,
  should_create_asset_group,
  _creator_parameters = nil
)
  # creates plates
  # creates an asset group if user requested one
  # prints the barcode labels

  @created_plates = []
  plate_purpose = plate_purposes.first
  plate_factories = tube_rack_to_plate_factories(tube_racks, plate_purpose)
  unless plate_factories.all?(&:valid?)
    errors = plate_factories.map(&:error_messages)
    fail_with_error("Plate creation failed with the following errors: #{errors}")
  end

  ActiveRecord::Base.transaction do
    plate_factories.each do |factory|
      factory.save
      add_created_plates(factory.tube_rack, [factory.plate])
    end
  end

  @created_asset_group = create_asset_group(created_plates) if should_create_asset_group

  print_job =
    LabelPrinter::PrintJob.new(
      barcode_printer.name,
      LabelPrinter::Label::PlateCreator,
      plates: created_plates.pluck(:destinations).flatten.compact,
      plate_purpose: plate_purpose,
      user_login: scanned_user.
    )

  warnings_list << 'Barcode labels failed to print.' unless print_job.execute
  true
end

#create_plates_from_tubes!(tubes, created_plates, scanned_user, barcode_printer) ⇒ void

This method returns an undefined value.

Creates plates from the given tubes and appends them to the created_plates array. If successfully created, sends a label printing job with plate parameters to the corresponding printing service.

This was declared a bang method as it mutates the receiver (i.e. the tubes list).

Parameters:

  • tubes (Array<Tube>)

    The array of tubes to be transferred to the plate.

  • created_plates (Array<Plate>)

    The array to store the created plates information.



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/plate/creator.rb', line 152

def create_plates_from_tubes!(tubes, created_plates, scanned_user, barcode_printer)
  plate_purpose = plate_purposes.first
  plate_barcode = PlateBarcode.create_barcode
  tubes_dup = tubes.dup
  plate = create_plate(plate_purpose, plate_barcode)
  return if plate.blank?

  duplicate_barcodes = process_tubes(tubes, plate)
  print_labels(plate, plate_purpose, barcode_printer, scanned_user)
  handle_duplicates(duplicate_barcodes)

  created_plates << { source: tubes_dup, destinations: [plate] }
end

#created_platesObject

array of hashes containing source and destination plates [ { :source => #<Plate …>, :destinations => [#<Plate …>, #<Plate …>] } ]



60
61
62
# File 'app/models/plate/creator.rb', line 60

def created_plates
  @created_plates ||= []
end

#execute(source_plate_barcodes, barcode_printer, scanned_user, should_create_asset_group, creator_parameters = nil) ⇒ Object

Executes the plate creation so that the appropriate child plates are built. rubocop:todo Metrics/MethodLength



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/plate/creator.rb', line 71

def execute(source_plate_barcodes, barcode_printer, scanned_user, should_create_asset_group, creator_parameters = nil)
  @created_plates = []

  new_plates = transaction { create_plates(source_plate_barcodes, scanned_user, creator_parameters) }
  fail_with_error('Plate creation failed') if new_plates.empty?

  new_plates
    .group_by(&:plate_purpose)
    .each do |plate_purpose, plates|
      print_job =
        LabelPrinter::PrintJob.new(
          barcode_printer.name,
          LabelPrinter::Label::PlateCreator,
          plates: plates,
          plate_purpose: plate_purpose,
          user_login: scanned_user.
        )

      unless print_job.execute
        warnings_list << "Barcode labels failed to print for following plate type: #{plate_purpose.name}"
      end
    end

  @created_asset_group = create_asset_group(created_plates) if should_create_asset_group
  true
end

#fail_with_error(msg) ⇒ Object

Raises:



64
65
66
67
# File 'app/models/plate/creator.rb', line 64

def fail_with_error(msg)
  @created_plates = []
  raise PlateCreationError, msg
end

#warningsObject



49
50
51
# File 'app/models/plate/creator.rb', line 49

def warnings
  warnings_list.join(' ')
end

#warnings_listObject



45
46
47
# File 'app/models/plate/creator.rb', line 45

def warnings_list
  @warnings_list ||= []
end