Class: ExtractionAttribute

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

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 Warren::BroadcastMessages

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

Instance Method Details

#attributes_update_with_resourcesObject


49
50
51
52
53
54
55
56
57
# File 'app/models/extraction_attribute.rb', line 49

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


71
72
73
74
75
# File 'app/models/extraction_attribute.rb', line 71

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


43
44
45
46
47
# File 'app/models/extraction_attribute.rb', line 43

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)

36
37
38
39
40
41
# File 'app/models/extraction_attribute.rb', line 36

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


67
68
69
# File 'app/models/extraction_attribute.rb', line 67

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

#rack_well(well_data) ⇒ Object


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/extraction_attribute.rb', line 89

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:


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
# File 'app/models/extraction_attribute.rb', line 107

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)

  # This block is enabled when we have the labware table present as part of the AssetRefactor
  # Ie. This is what will happen in future
  AssetRefactor.when_refactored do
    # 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!
  end
  well.update!(plate: actual_parent, map: actual_map)
end

#validate_well_for_racking_samples!(destination_well, samples) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/extraction_attribute.rb', line 77

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.include?(sample) }
end