Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
gy4443
chemotion_eln_server
Commits
8fb77ba0
Commit
8fb77ba0
authored
Oct 13, 2015
by
Marco Sehrer
Browse files
Merge pull request #179 from ninjaconcept/179-delete-material-on-reaction-update
Reaction update for deleted samples
parents
18193568
71b3464a
Changes
7
Hide whitespace changes
Inline
Side-by-side
app/api/chemotion/reaction_api.rb
View file @
8fb77ba0
...
...
@@ -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
...
...
app/assets/javascripts/components/ReactionDetailsScheme.js
View file @
8fb77ba0
...
...
@@ -22,25 +22,20 @@ export default class ReactionDetailsScheme extends Component {
dropSample
(
sample
,
materialGroup
)
{
const
{
reaction
}
=
this
.
state
;
const
materials
=
reaction
[
materialGroup
];
const
splitSample
=
Sample
.
buildChild
(
sample
);
m
aterial
s
.
push
(
splitSample
);
reaction
.
addM
aterial
(
splitSample
,
materialGroup
);
this
.
onReactionChange
(
reaction
,
{
schemaChanged
:
true
});
}
deleteMaterial
(
material
,
materialGroup
)
{
const
{
reaction
}
=
this
.
state
;
const
materials
=
reaction
[
materialGroup
];
const
materialIndex
=
materials
.
indexOf
(
material
);
materials
.
splice
(
materialIndex
,
1
);
let
{
reaction
}
=
this
.
state
;
reaction
.
deleteMaterial
(
material
,
materialGroup
);
this
.
onReactionChange
(
reaction
,
{
schemaChanged
:
true
});
}
dropMaterial
(
material
,
previousMaterialGroup
,
materialGroup
)
{
const
{
reaction
}
=
this
.
state
;
const
materials
=
reaction
[
materialGroup
];
this
.
deleteMaterial
(
material
,
previousMaterialGroup
);
materials
.
push
(
material
);
reaction
.
moveMaterial
(
material
,
previousMaterialGroup
,
materialGroup
);
this
.
onReactionChange
(
reaction
,
{
schemaChanged
:
true
});
}
...
...
app/assets/javascripts/components/models/Reaction.js
View file @
8fb77ba0
...
...
@@ -77,6 +77,23 @@ export default class Reaction {
return
[...
this
.
starting_materials
,
...
this
.
reactants
,
...
this
.
products
]
}
addMaterial
(
material
,
materialGroup
)
{
const
materials
=
this
[
materialGroup
];
materials
.
push
(
material
);
}
deleteMaterial
(
material
,
materialGroup
)
{
const
materials
=
this
[
materialGroup
];
const
materialIndex
=
materials
.
indexOf
(
material
);
materials
.
splice
(
materialIndex
,
1
);
}
moveMaterial
(
material
,
previousMaterialGroup
,
materialGroup
)
{
const
materials
=
this
[
materialGroup
];
this
.
deleteMaterial
(
material
,
previousMaterialGroup
);
materials
.
push
(
material
);
}
_coerceToSamples
(
samples
)
{
return
samples
&&
samples
.
map
(
s
=>
new
Sample
(
s
))
||
[]
}
...
...
app/models/reaction.rb
View file @
8fb77ba0
...
...
@@ -71,18 +71,16 @@ class Reaction < ActiveRecord::Base
def
update_svg_file!
inchikeys
=
{}
inchikeys
[
:starting_materials
]
=
starting_materials
.
map
do
|
material
|
material
.
molecule
.
inchikey
end
inchikeys
[
:reactants
]
=
reactants
.
map
do
|
material
|
material
.
molecule
.
inchikey
end
inchikeys
[
:products
]
=
products
.
map
do
|
material
|
material
.
molecule
.
inchikey
end
inchikeys
[
:starting_materials
]
=
starting_materials
.
includes
(
:molecule
).
pluck
(
:'molecules.inchikey'
)
inchikeys
[
:reactants
]
=
reactants
.
includes
(
:molecule
).
pluck
(
:'molecules.inchikey'
)
inchikeys
[
:products
]
=
products
.
includes
(
:molecule
).
pluck
(
:'molecules.inchikey'
)
label
=
[
solvents
,
temperature
].
compact
.
join
(
', '
)
composer
=
SVG
::
ReactionComposer
.
new
(
inchikeys
,
label:
label
)
self
.
reaction_svg_file
=
composer
.
compose_reaction_svg_and_save
begin
composer
=
SVG
::
ReactionComposer
.
new
(
inchikeys
,
label:
label
)
self
.
reaction_svg_file
=
composer
.
compose_reaction_svg_and_save
rescue
Exception
=>
e
p
"**** SVG::ReactionComposer failed ***"
end
end
end
app/models/sample.rb
View file @
8fb77ba0
...
...
@@ -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
...
...
db/seeds.rb
View file @
8fb77ba0
...
...
@@ -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
)
...
...
spec/api/reaction_api_spec.rb
View file @
8fb77ba0
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment