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

Instance Method Details

#attributes_update_with_resourcesObject


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

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


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

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

#find_resources(attr_well, attr_well_uuid_key) ⇒ Object


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

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)

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

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


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

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

#rack_well(well_data) ⇒ Object

rubocop:todo Metrics/MethodLength


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

def rack_well(well_data) # rubocop:todo Metrics/MethodLength
  return unless well_data && well_data['sample_tube_uuid']
  raise SampleTubeNotExists unless well_data['sample_tube_resource']

  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

rubocop:todo Metrics/AbcSize

Raises:


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

def rerack_well(well_data) # rubocop:todo Metrics/AbcSize
  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


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

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