Commit 5231d89c authored by Marco Sehrer's avatar Marco Sehrer
Browse files

Get rid off 'AllCollection'

parent 02274564
......@@ -47,6 +47,7 @@ module Chemotion
Collection.find(params[:id]).samples
end
end
end
end
end
......@@ -3,6 +3,20 @@ module Chemotion
# TODO ensure user is authenticated
resource :samples do
#todo: more general search api
desc "Return serialized samples"
params do
optional :collection_id, type: Integer, desc: "Collection id"
end
get do
if(params[:collection_id])
current_user.collections.find(params[:collection_id]).samples
else
Sample.all
end
end
desc "Return serialized sample by id"
params do
requires :id, type: Integer, desc: "Sample id"
......@@ -13,6 +27,7 @@ module Chemotion
end
end
desc "Update sample by id"
params do
requires :id, type: Integer, desc: "Sample id"
......@@ -27,6 +42,8 @@ module Chemotion
amount_unit: params[:amount_unit]
})
end
end
end
end
......@@ -63,8 +63,15 @@ export default class CollectionTree extends React.Component {
}
render() {
let allCollection = {
label: 'All *',
id: 'all',
children: []
};
return (
<div>
<CollectionSubtree key='all' root={allCollection}/>
<div className="tree-wrapper">
{this.unsharedSubtrees()}
</div>
......
......@@ -19,9 +19,7 @@ class ElementActions {
fetchSamplesByCollectionId(id) {
SamplesFetcher.fetchByCollectionId(id)
.then((result) => {
// TODO adjust when CollectionAPI fixed
// (serializer nests results within :id)
this.dispatch(result[':id']);
this.dispatch(result['samples']);
}).catch((errorMessage) => {
console.log(errorMessage);
});
......
......@@ -19,6 +19,7 @@ class UIActions {
}
selectElement(element) {
console.log("selectElement:" + element.id + " type: " + element.type)
this.dispatch(element);
}
......
......@@ -18,7 +18,8 @@ export default class SamplesFetcher {
}
static fetchByCollectionId(id) {
let promise = fetch('/api/v1/collections/' + id + '/samples.json', {
let api = id == 'all' ? '/api/v1/samples.json' : '/api/v1/samples.json?collection_id=' + id
let promise = fetch(api, {
credentials: 'same-origin'
})
.then((response) => {
......
......@@ -9,7 +9,4 @@ class Collection < ActiveRecord::Base
scope :shared, ->(user_id) { where(shared_by_id: user_id) }
scope :remote, ->(user_id) { where(is_shared: true) && where.not(shared_by_id: user_id) }
def is_all_collection?
label == 'All'
end
end
class CollectionsReaction < ActiveRecord::Base
belongs_to :collection
belongs_to :reaction
after_create :add_to_all_collection
# TODO maybe not the best way to handle the 'all' collection
def add_to_all_collection
unless Collection.find(collection_id).is_all_collection?
user_id = Collection.find(collection_id).user_id
all_collection = Collection.find_by(label: 'All', user_id: user_id)
CollectionsReaction.create!(collection_id: all_collection.id, reaction_id: reaction_id)
end
end
end
class CollectionsSample < ActiveRecord::Base
belongs_to :collection
belongs_to :sample
after_create :add_to_all_collection
private
def add_to_all_collection
unless Collection.find(collection_id).is_all_collection?
user_id = Collection.find(collection_id).user_id
all_collection = Collection.find_or_create_by(label: 'All', user_id: user_id)
CollectionsSample.create!(collection_id: all_collection.id, sample_id: sample_id)
end
end
end
......@@ -3,8 +3,6 @@ u = User.create!(email: 'test@ninjaconcept.com', password: 'ninjaconcept', passw
hattori = User.create!(email: 'hattori@ninjaconcept.com', password: 'ninjaconcept', password_confirmation: 'ninjaconcept')
momochi = User.create!(email: 'momochi@ninjaconcept.com', password: 'ninjaconcept', password_confirmation: 'ninjaconcept')
# create some collections
all_collection = Collection.create!(label: 'All', user_id: u.id)
collection_1 = Collection.create!(label: 'Collection #1', user_id: u.id)
subcollection_1 = Collection.create!(label: 'Subcollection of #1', user_id: u.id, parent: collection_1)
grand_child = Collection.create!(label: 'Grandchild', user_id: u.id, parent: subcollection_1)
......
Supports Markdown
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