Commit 7db83907 authored by Marco Sehrer's avatar Marco Sehrer
Browse files

Delete all materials not included in reaction update

parent 5452cde3
......@@ -177,10 +177,12 @@ module ReactionUpdator
}
ActiveRecord::Base.transaction do
included_sample_ids = []
materials.each do |material_group, samples|
reaction_samples_association = reaction.public_send("reactions_#{material_group}_samples")
samples.each do |sample|
#create new subsample
if sample.is_new && sample.parent_id
parent_sample = Sample.find(sample.parent_id)
......@@ -192,8 +194,9 @@ module ReactionUpdator
subsample.amount_value = sample.amount_value
subsample.amount_unit = sample.amount_unit
subsample.id
subsample.save
subsample.reload
included_sample_ids << subsample.id
#assign subsample to current collection
CollectionsSample.create(collection_id: collection_id, sample_id: subsample.id)
......@@ -203,12 +206,15 @@ module ReactionUpdator
equivalent: sample.equivalent,
reference: sample.reference
)
#update the existing sample
else
existing_sample = Sample.find(sample.id)
existing_sample.amount_value = sample.amount_value
existing_sample.amount_unit = sample.amount_unit
existing_sample.save
included_sample_ids << existing_sample.id
existing_association = reaction_samples_association.find_by(sample_id: sample.id)
......@@ -218,6 +224,7 @@ module ReactionUpdator
equivalent: sample.equivalent,
reference: sample.reference
)
#sample was moved to other materialgroup
else
#clear existing associations
reaction.reactions_starting_material_samples.find_by(sample_id: sample.id).try(:destroy)
......@@ -231,10 +238,22 @@ module ReactionUpdator
reference: sample.reference
)
end
end
end
end
#delete all samples not anymore in one of the groups
current_sample_ids = [
reaction.reactions_starting_material_samples.pluck(:sample_id),
reaction.reactions_reactant_samples.pluck(:sample_id),
reaction.reactions_product_samples.pluck(:sample_id)
].flatten.uniq
deleted_sample_ids = current_sample_ids - included_sample_ids
Sample.where(id: deleted_sample_ids).destroy_all
#for testing
#raise ActiveRecord::Rollback
end
......
......@@ -38,9 +38,9 @@ class Sample < ActiveRecord::Base
has_many :collections_samples
has_many :collections, through: :collections_samples
has_many :reactions_starting_material_samples
has_many :reactions_reactant_samples
has_many :reactions_product_samples
has_many :reactions_starting_material_samples, dependent: :destroy
has_many :reactions_reactant_samples, dependent: :destroy
has_many :reactions_product_samples, dependent: :destroy
has_many :reactions_as_starting_material, through: :reactions_starting_material_samples, source: :reaction
has_many :reactions_as_reactant, through: :reactions_reactant_samples, source: :reaction
......
......@@ -139,18 +139,18 @@ reaction_4 = Reaction.create(name: 'Reaction 4')
reaction_5 = Reaction.create(name: 'Reaction 5')
reaction_6 = Reaction.create(name: 'Reaction 6')
# associate samples with reactions
ReactionsStartingMaterialSample.create!(reaction: reaction_1, sample: sample_1, reference: true, equivalent: 1)
ReactionsReactantSample.create!(reaction: reaction_1, sample: sample_2, equivalent: 2)
ReactionsProductSample.create!(reaction: reaction_1, sample: sample_3, equivalent: 1)
reaction_1.reload
reaction_1.save
ReactionsStartingMaterialSample.create!(reaction: reaction_2, sample: sample_3, equivalent: 1)
ReactionsReactantSample.create!(reaction: reaction_2, sample: sample_2, equivalent: 2)
ReactionsProductSample.create!(reaction: reaction_2, sample: sample_1, equivalent: 3)
reaction_2.reload
reaction_2.save
# # associate samples with reactions
# ReactionsStartingMaterialSample.create!(reaction: reaction_1, sample: sample_1, reference: true, equivalent: 1)
# ReactionsReactantSample.create!(reaction: reaction_1, sample: sample_2, equivalent: 2)
# ReactionsProductSample.create!(reaction: reaction_1, sample: sample_3, equivalent: 1)
# reaction_1.reload
# reaction_1.save
#
# ReactionsStartingMaterialSample.create!(reaction: reaction_2, sample: sample_3, equivalent: 1)
# ReactionsReactantSample.create!(reaction: reaction_2, sample: sample_2, equivalent: 2)
# ReactionsProductSample.create!(reaction: reaction_2, sample: sample_1, equivalent: 3)
# reaction_2.reload
# reaction_2.save
# associate reactions with collections
CollectionsReaction.create!(reaction: reaction_1, collection: collection_1)
......
......@@ -190,6 +190,7 @@ describe Chemotion::ReactionAPI do
let(:sample_1) { Sample.create!(name: 'Sample 1') }
let(:sample_2) { Sample.create!(name: 'Sample 2') }
let(:sample_3) { Sample.create!(name: 'Sample 3') }
let(:sample_4) { Sample.create!(name: 'Sample 4') }
let(:reaction_1) { Reaction.create(name: 'r1') }
before do
......@@ -197,6 +198,7 @@ describe Chemotion::ReactionAPI do
ReactionsStartingMaterialSample.create!(reaction: reaction_1, sample: sample_1, reference: true, equivalent: 1)
ReactionsReactantSample.create!(reaction: reaction_1, sample: sample_2, equivalent: 2)
ReactionsProductSample.create!(reaction: reaction_1, sample: sample_3, equivalent: 1)
ReactionsProductSample.create!(reaction: reaction_1, sample: sample_4, equivalent: 1)
end
context 'updating and reassigning existing materials' do
......@@ -222,6 +224,16 @@ describe Chemotion::ReactionAPI do
"reference" => false,
"is_new" => false
}
],
"products" => [
{
"id" => sample_3.id,
"amount_unit" => "mg",
"amount_value" => 99.08404,
"equivalent" => 5.5,
"reference" => false,
"is_new" => false
}
]
}
}
......@@ -253,7 +265,6 @@ describe Chemotion::ReactionAPI do
end
it 'should material associations and reassign to a new group' do
sa1 = r.reactions_starting_material_samples.find_by(sample_id: sample_1.id)
sa2 = r.reactions_starting_material_samples.find_by(sample_id: sample_2.id)
......@@ -268,8 +279,13 @@ describe Chemotion::ReactionAPI do
})
expect(r.reactions_reactant_samples).to be_empty
end
it 'should delete only not included samples' do
expect(r.reactions_product_samples.find_by(sample_id: sample_3.id)).to be_present
expect(r.reactions_product_samples.find_by(sample_id: sample_4.id)).not_to be_present
end
end
context 'creating new materials' do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment