Skip to content

Commit 160caeb

Browse files
authored
Merge pull request #1363 from sanger/1354-y24-180---bug-pacbio-libraries-not-updating-their-used_aliquots-when-they-are-edited
feat: Update used aliquots data when primary aliquot is updated for a library
2 parents 9e9a269 + 91330f0 commit 160caeb

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

app/models/pacbio/library.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class Library < ApplicationRecord
4949
# if the new volume is greater than the used volume.
5050
before_update :primary_aliquot_volume_sufficient
5151

52+
after_update :update_used_aliquots
53+
5254
before_destroy :check_for_derived_aliquots, prepend: true
5355

5456
def create_used_aliquot
@@ -65,6 +67,19 @@ def create_used_aliquot
6567
)
6668
end
6769

70+
# Updates the used aliquots data when the primary aliquot is updated
71+
def update_used_aliquots
72+
used_aliquots.each do |aliquot|
73+
aliquot.update(
74+
volume: primary_aliquot.volume,
75+
concentration: primary_aliquot.concentration,
76+
template_prep_kit_box_barcode: primary_aliquot.template_prep_kit_box_barcode,
77+
insert_size: primary_aliquot.insert_size,
78+
tag: primary_aliquot.tag
79+
)
80+
end
81+
end
82+
6883
# Always false for libraries, but always true for wells - a gross simplification
6984
def collection?
7085
false
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
namespace :used_aliquots do
4+
task update_tags: :environment do
5+
libraries = Pacbio::Library.all.reject { |library| library.used_aliquots.first.tag == library.tag }
6+
libraries.each do |library|
7+
library.used_aliquots.each do |used_aliquot|
8+
used_aliquot.update!(tag: library.tag)
9+
end
10+
end
11+
puts "-> #{libraries.count} instances of libraries updated."
12+
end
13+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
require 'rake'
5+
6+
# only load Rake tasks if they haven't been loaded already
7+
Rails.application.load_tasks if Rake::Task.tasks.empty?
8+
9+
RSpec.describe 'RakeTasks' do
10+
describe 'used_aliquots:update_tags' do
11+
it 'updates used aliquots with primary aliquot tag' do
12+
libraries = create_list(:pacbio_library, 5)
13+
tag = create(:tag)
14+
libraries.each do |library|
15+
library.used_aliquots.first.update!(tag:)
16+
end
17+
18+
expect { Rake::Task['used_aliquots:update_tags'].invoke }.to output(
19+
"-> #{libraries.count} instances of libraries updated.\n"
20+
).to_stdout
21+
22+
libraries.each do |library|
23+
library.reload
24+
expect(library.used_aliquots.first.tag).to eq(library.tag)
25+
end
26+
end
27+
end
28+
end

spec/models/pacbio/library_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,17 @@
310310
library.save
311311
end
312312
end
313+
314+
context 'after_update' do
315+
it 'updates used_aliquots' do
316+
library = create(:pacbio_library)
317+
tag = create(:tag)
318+
library.update(primary_aliquot_attributes: { tag_id: tag.id, volume: 1000, concentration: 1000, insert_size: 1000, template_prep_kit_box_barcode: 'LK456' })
319+
expect(library.used_aliquots.first.tag).to eq(tag)
320+
expect(library.used_aliquots.first.volume).to eq(1000)
321+
expect(library.used_aliquots.first.concentration).to eq(1000)
322+
expect(library.used_aliquots.first.insert_size).to eq(1000)
323+
expect(library.used_aliquots.first.template_prep_kit_box_barcode).to eq('LK456')
324+
end
325+
end
313326
end

spec/requests/v1/pacbio/libraries_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,6 @@
457457
library.reload
458458
expect(library.primary_aliquot.template_prep_kit_box_barcode).to eq('100')
459459
end
460-
461-
it 'does not update the used_aliquots' do
462-
library.reload
463-
expect(library.used_aliquots.first.template_prep_kit_box_barcode).not_to eq('100')
464-
end
465460
end
466461

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

0 commit comments

Comments
 (0)