Class: ExtractionAttribute

Inherits:
ApplicationRecord show all
Includes:
Uuid::Uuidable
Defined in:
app/models/extraction_attribute.rb

Overview

rubocop:todo Style/Documentation

Defined Under Namespace

Classes: SampleTubeNotExists, WellAlreadyHasSample, WellNotExists

Instance Method Summary collapse

Methods included from Uuid::Uuidable

included, #unsaved_uuid!, #uuid

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 Method Details

#attributes_update_with_resourcesObject


38
39
40
41
42
43
44
45
46
# File 'app/models/extraction_attribute.rb', line 38

def attributes_update_with_resources
  attributes_update.map do |attr_well|
    resources = {
      'resource' => find_resources(attr_well, 'uuid'),
      'sample_tube_resource' => find_resources(attr_well, 'sample_tube_uuid')
    }.compact
    attr_well.merge(resources)
  end
end

#disallow_wells_with_multiple_samples!(destination_well, samples) ⇒ Object


60
61
62
63
64
# File 'app/models/extraction_attribute.rb', line 60

def disallow_wells_with_multiple_samples!(destination_well, samples)
  if (destination_well.samples.count > 0) && (destination_well.samples != samples)
    raise WellAlreadyHasSample
  end
end

#find_resources(attr_well, attr_well_uuid_key) ⇒ Object


32
33
34
35
36
# File 'app/models/extraction_attribute.rb', line 32

def find_resources(attr_well, attr_well_uuid_key)
  return unless attr_well

  Uuid.find_by(external_id: attr_well[attr_well_uuid_key]).resource if attr_well[attr_well_uuid_key]
end

#is_reracking?(well_info) ⇒ Boolean

Returns:

  • (Boolean)

25
26
27
28
29
30
# File 'app/models/extraction_attribute.rb', line 25

def is_reracking?(well_info)
  well = well_info['resource']
  return false unless well

  (well.plate != target) || (well.map_description != well_info['location'])
end

#location_wellsObject


56
57
58
# File 'app/models/extraction_attribute.rb', line 56

def location_wells
  target.wells.includes(:map, :samples, :aliquots).index_by(&:map_description)
end

#rack_well(well_data) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/extraction_attribute.rb', line 78

def rack_well(well_data)
  return unless well_data && well_data['sample_tube_uuid']
  unless well_data['sample_tube_resource']
    raise SampleTubeNotExists
  end

  sample_tube = well_data['sample_tube_resource']
  aliquots = sample_tube.aliquots.map(&:dup)
  samples = sample_tube.samples
  location = well_data['location']
  destination_well = location_wells[location]

  if validate_well_for_racking_samples!(destination_well, samples)
    destination_well.aliquots << aliquots
    AssetLink.create_edge(sample_tube, destination_well)
  end
end

#rerack_well(well_data) ⇒ Object

Raises:


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/extraction_attribute.rb', line 96

def rerack_well(well_data)
  return unless well_data

  well = well_data['resource']

  actual_parent = target
  location = well_data['location']
  actual_well_in_same_position_at_rack = target.wells.located_at(location).first
  actual_map = target.maps.detect { |m| m.description == location }
  raise WellNotExists if actual_map.nil?

  actual_well_in_same_position_at_rack&.update!(plate: nil)

  # If an earlier well was moved into THIS wells previous location then
  # it will have been removed from the plate. HOWEVER, because this happens on
  # a DIFFERENT object, (as it gets found in a separate query) then this particular
  # instance of well has no way of knowing that this change has been made. This is
  # particularly problematic post-re-factor, as it results in the plate relationship
  # not getting flagged as dirty, and so not updating. As a result the update for the
  # earlier well takes precedence, and the location remains nil.
  # Container_associations didn't result in the same problem
  well.labware_id_will_change!

  well.update!(plate: actual_parent, map: actual_map)
end

#validate_well_for_racking_samples!(destination_well, samples) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/extraction_attribute.rb', line 66

def validate_well_for_racking_samples!(destination_well, samples)
  unless destination_well
    # TO RESEARCH:
    # If the well does not exist (because, for instance, it was reracked), we dont have
    # a well to rack. We should create a new well. For the moment, we'll fail in this situation
    raise WellNotExists
  end

  disallow_wells_with_multiple_samples!(destination_well, samples)
  samples.all? { |sample| destination_well.samples.exclude?(sample) }
end