Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/labimotion/apis/generic_element_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ class GenericElementAPI < Grape::API
desc 'Return serialized elements of current user'
params do
optional :collection_id, type: Integer, desc: 'Collection id'
optional :sync_collection_id, type: Integer, desc: 'SyncCollectionsUser id'
optional :el_type, type: String, desc: 'element klass name'
optional :from_date, type: Integer, desc: 'created_date from in ms'
optional :to_date, type: Integer, desc: 'created_date to in ms'
Expand Down
44 changes: 19 additions & 25 deletions lib/labimotion/helpers/element_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def create_element(current_user, params)
element = Labimotion::Element.new(attributes)

if params[:collection_id]
collection = current_user.collections.find(params[:collection_id])
collection = Collection.accessible_for(current_user).find(params[:collection_id])
element.collections << collection
end
all_coll = Collection.get_all_collection_for_user(current_user.id)
all_coll = Collection.get_all_collection_for_user(current_user)
element.collections << all_coll
element.save!
element.properties = update_sample_association(params[:properties], current_user, element)
Expand Down Expand Up @@ -246,38 +246,32 @@ def list_user_elements(scope, params)
end

def list_serialized_elements(params, current_user)
collection_id =
if params[:collection_id]
Collection
.belongs_to_or_shared_by(current_user.id, current_user.group_ids)
.find_by(id: params[:collection_id])&.id
elsif params[:sync_collection_id]
current_user
.all_sync_in_collections_users
.find_by(id: params[:sync_collection_id])&.collection&.id
end

scope =
if collection_id
Labimotion::Element
.joins(:element_klass, :collections_elements)
.where(
element_klasses: { name: params[:el_type] },
collections_elements: { collection_id: collection_id },
).includes(:tag, collections: :sync_collections_users)
else
Labimotion::Element.none
end
if params[:collection_id]
begin
Collection.accessible_for(current_user)
.find(params[:collection_id]).elements
.joins(:element_klass)
.where(element_klasses: { name: params[:el_type] })
rescue ActiveRecord::RecordNotFound
Labimotion::Element.none
end
else
Labimotion::Element.joins(:collections, :element_klass)
.where(collections: { user_id: current_user.id })
.where(element_klasses: { name: params[:el_type] })
.distinct
end

## TO DO: refactor labimotion
from = params[:from_date]
to = params[:to_date]
by_created_at = params[:filter_created_at] || false

if params[:sort_column]&.include?('.')
layer, field = params[:sort_column].split('.')

element_klass = Labimotion::ElementKlass.find_by(name: params[:el_type])
allowed_fields = element_klass.properties_release.dig(Labimotion::Prop::LAYERS, layer, Labimotion::Prop::FIELDS)&.pluck('field') || []
allowed_fields = element_klass&.properties_release&.dig(Labimotion::Prop::LAYERS, layer, Labimotion::Prop::FIELDS)&.pluck('field') || []

if field.in?(allowed_fields)
query = ActiveRecord::Base.sanitize_sql(
Expand Down
4 changes: 2 additions & 2 deletions lib/labimotion/models/concerns/element_fetchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def fetch_for_user(user_id, name: nil, short_label: nil, limit: 20)
joins(collections: :user).where(collections: { user_id: user_id })
)

# Shared (synced) records
# Shared records
shared = apply_filters.call(
joins(collections: :sync_collections_users).where(sync_collections_users: { user_id: user_id })
left_joins(:collection_shares).where(collection_shares: { shared_with_id: user_id })
)

# Combine (remove duplicates), order, and limit
Expand Down
4 changes: 2 additions & 2 deletions lib/labimotion/models/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ def self.fetch_for_user(user_id, name: nil, short_label: nil, klass_id: nil, lim
joins(collections: :user).where(collections: { user_id: user_id })
)

# Shared (synced) elements
# Shared elements
shared = apply_filters.call(
joins(collections: :sync_collections_users).where(sync_collections_users: { user_id: user_id })
left_joins(:collection_shares).where(collection_shares: { shared_with_id: user_id })
)

# Combine (remove duplicates), order, and limit
Expand Down
7 changes: 4 additions & 3 deletions lib/labimotion/utils/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def self.unique_segment_layers(id)
end

def self.elements_search(params, current_user, c_id, dl)
collection = Collection.belongs_to_or_shared_by(current_user.id, current_user.group_ids).find(c_id)
collection = Collection.accessible_for(current_user).find(c_id)
element_scope = Labimotion::Element.joins(:collections_elements).where('collections_elements.collection_id = ?', collection.id).joins(:element_klass).where('element_klasses.id = elements.element_klass_id AND element_klasses.name = ?', params[:selection][:genericElName])
element_scope = element_scope.where('elements.name like (?)', "%#{params[:selection][:searchName]}%") if params[:selection][:searchName].present?
element_scope = element_scope.where('elements.short_label like (?)', "%#{params[:selection][:searchShowLabel]}%") if params[:selection][:searchShowLabel].present?
element_scope = element_scope.where('elements.short_label like (?)', "%#{params[:selection][:searchShowLabel]}%")
if params[:selection][:searchShowLabel].present?
if params[:selection][:searchProperties].present?
unique_layers = Labimotion::Search.unique_element_layers(params[:selection] && params[:selection][:genericKlassId])
params[:selection][:searchProperties] && params[:selection][:searchProperties][:layers] && params[:selection][:searchProperties][:layers].keys.each do |lk|
Expand Down Expand Up @@ -97,7 +98,7 @@ def self.segments_search(ids, type)
def self.samples_search(c_id = @c_id)
sqls = []
sps = params[:selection][:searchProperties]
collection = Collection.belongs_to_or_shared_by(current_user.id, current_user.group_ids).find(c_id)
collection = Collection.accessible_for(current_user).find(c_id)
element_scope = Sample.joins(:collections_samples).where('collections_samples.collection_id = ?', collection.id)
return element_scope if sps.empty?

Expand Down