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_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

#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