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 =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Instance Attribute Details

#created_asset_groupObject (readonly)

Returns the value of attribute created_asset_group


37
38
39
# File 'app/models/plate/creator.rb', line 37

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


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/plate/creator.rb', line 84

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 ...>]
}

]


54
55
56
# File 'app/models/plate/creator.rb', line 54

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.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/plate/creator.rb', line 64

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

  new_plates = transaction do
    create_plates(source_plate_barcodes, scanned_user, creator_parameters)
  end
  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.)

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

  @created_asset_group = create_asset_group(created_plates) if should_create_asset_group
  true
end

#fail_with_error(msg) ⇒ Object

Raises:


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

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

#warningsObject


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

def warnings
  warnings_list.join(' ')
end

#warnings_listObject


39
40
41
# File 'app/models/plate/creator.rb', line 39

def warnings_list
  @warnings_list ||= []
end