Skip to content

Commit

Permalink
Merge pull request #1363 from sanger/1354-y24-180---bug-pacbio-librar…
Browse files Browse the repository at this point in the history
…ies-not-updating-their-used_aliquots-when-they-are-edited

feat: Update used aliquots data when primary aliquot is updated for a library
  • Loading branch information
stevieing authored Jul 23, 2024
2 parents 9e9a269 + 91330f0 commit 160caeb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
15 changes: 15 additions & 0 deletions app/models/pacbio/library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Library < ApplicationRecord
# if the new volume is greater than the used volume.
before_update :primary_aliquot_volume_sufficient

after_update :update_used_aliquots

before_destroy :check_for_derived_aliquots, prepend: true

def create_used_aliquot
Expand All @@ -65,6 +67,19 @@ def create_used_aliquot
)
end

# Updates the used aliquots data when the primary aliquot is updated
def update_used_aliquots
used_aliquots.each do |aliquot|
aliquot.update(
volume: primary_aliquot.volume,
concentration: primary_aliquot.concentration,
template_prep_kit_box_barcode: primary_aliquot.template_prep_kit_box_barcode,
insert_size: primary_aliquot.insert_size,
tag: primary_aliquot.tag
)
end
end

# Always false for libraries, but always true for wells - a gross simplification
def collection?
false
Expand Down
13 changes: 13 additions & 0 deletions lib/tasks/update_used_aliquots_with_primary_aliquot_tags.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

namespace :used_aliquots do
task update_tags: :environment do
libraries = Pacbio::Library.all.reject { |library| library.used_aliquots.first.tag == library.tag }
libraries.each do |library|
library.used_aliquots.each do |used_aliquot|
used_aliquot.update!(tag: library.tag)
end
end
puts "-> #{libraries.count} instances of libraries updated."
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'rails_helper'
require 'rake'

# only load Rake tasks if they haven't been loaded already
Rails.application.load_tasks if Rake::Task.tasks.empty?

RSpec.describe 'RakeTasks' do
describe 'used_aliquots:update_tags' do
it 'updates used aliquots with primary aliquot tag' do
libraries = create_list(:pacbio_library, 5)
tag = create(:tag)
libraries.each do |library|
library.used_aliquots.first.update!(tag:)
end

expect { Rake::Task['used_aliquots:update_tags'].invoke }.to output(
"-> #{libraries.count} instances of libraries updated.\n"
).to_stdout

libraries.each do |library|
library.reload
expect(library.used_aliquots.first.tag).to eq(library.tag)
end
end
end
end
13 changes: 13 additions & 0 deletions spec/models/pacbio/library_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,17 @@
library.save
end
end

context 'after_update' do
it 'updates used_aliquots' do
library = create(:pacbio_library)
tag = create(:tag)
library.update(primary_aliquot_attributes: { tag_id: tag.id, volume: 1000, concentration: 1000, insert_size: 1000, template_prep_kit_box_barcode: 'LK456' })
expect(library.used_aliquots.first.tag).to eq(tag)
expect(library.used_aliquots.first.volume).to eq(1000)
expect(library.used_aliquots.first.concentration).to eq(1000)
expect(library.used_aliquots.first.insert_size).to eq(1000)
expect(library.used_aliquots.first.template_prep_kit_box_barcode).to eq('LK456')
end
end
end
5 changes: 0 additions & 5 deletions spec/requests/v1/pacbio/libraries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,6 @@
library.reload
expect(library.primary_aliquot.template_prep_kit_box_barcode).to eq('100')
end

it 'does not update the used_aliquots' do
library.reload
expect(library.used_aliquots.first.template_prep_kit_box_barcode).not_to eq('100')
end
end

context 'on failure - when there is invalid library data' do
Expand Down

0 comments on commit 160caeb

Please sign in to comment.