<commit_before>module Usecases::Wellplates::WellplateUpdater
  def self.update_wells_for_wellplate(wellplate, wells)
    collections = wellplate.collections
    current_sample_ids = wellplate.wells.pluck(:sample_id).uniq.compact
    included_sample_ids = []

    wells.each do |well|
      sample = well.sample
      sample_id = sample && sample.id

      if sample
        if sample.is_new && sample.parent_id
          parent_sample = Sample.find(sample.parent_id)

          subsample = parent_sample.dup
          subsample.parent = parent_sample
          subsample.short_label = nil #we don't want to inherit short_label from parent
          subsample.name = sample.name

          #assign subsample to all collections
          subsample.collections << collections
          subsample.save!
          subsample.reload

          sample_id = subsample.id
        end
        included_sample_ids << sample_id
      end


      unless well.is_new
        Well.find(well.id).update(
            sample_id: sample_id,
            readout: well.readout,
            additive: well.additive,
            position_x: well.position.x,
            position_y: well.position.y,
        )
      else
        Well.create(
          wellplate_id: wellplate.id,
          sample_id: sample_id,
          readout: well.readout,
          additive: well.additive,
          position_x: well.position.x,
          position_y: well.position.y,
        )
      end
    end

    deleted_sample_ids = current_sample_ids - included_sample_ids
    Sample.where(id: deleted_sample_ids).destroy_all
  end
end
<commit_msg>Use hash access<commit_after>module Usecases::Wellplates::WellplateUpdater
  def self.update_wells_for_wellplate(wellplate, wells)
    collections = wellplate.collections
    current_sample_ids = wellplate.wells.pluck(:sample_id).uniq.compact
    included_sample_ids = []

    wells.each do |well|
      sample = well[:sample]
      sample_id = sample && sample[:id]

      if sample
        if sample[:is_new] && sample[:parent_id]
          parent_sample = Sample.find(sample[:parent_id])

          subsample = parent_sample.dup
          subsample.parent = parent_sample
          subsample.short_label = nil #we don't want to inherit short_label from parent
          subsample.name = sample[:name]

          #assign subsample to all collections
          subsample.collections << collections
          subsample.save!
          subsample.reload

          sample_id = subsample.id
        end
        included_sample_ids << sample_id
      end


      unless well[:is_new]
        Well.find(well[:id]).update(
            sample_id: sample_id,
            readout: well[:readout],
            additive: well[:additive],
            position_x: well[:position][:x],
            position_y: well[:position][:y],
        )
      else
        Well.create(
          wellplate_id: wellplate.id,
          sample_id: sample_id,
          readout: well[:readout],
          additive: well[:additive],
          position_x: well[:position][:x],
          position_y: well[:position][:y],
        )
      end
    end

    deleted_sample_ids = current_sample_ids - included_sample_ids
    Sample.where(id: deleted_sample_ids).destroy_all
  end
end

{'fraction_non_alphanumeric': 0.08435960591133004, 'fraction_numerical': 0.0, 'mean_word_length': 2.22001982160555, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1778964', 'n_tokens_mistral': 967, 'n_tokens_neox': 934, 'n_words': 186}