Module: Labimotion::ElementHelpers

Extended by:
Grape::API::Helpers
Defined in:
lib/labimotion/helpers/element_helpers.rb

Overview

ElementHelpers

Instance Method Summary collapse

Instance Method Details

#create_element(current_user, params, version) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/labimotion/helpers/element_helpers.rb', line 57

def create_element(current_user, params, version)
  klass = params[:element_klass] || {}
  uuid = SecureRandom.uuid
  params[:properties]['uuid'] = uuid
  params[:properties]['klass_uuid'] = klass[:uuid]
  params[:properties]['eln'] = Chemotion::Application.config.version
  params[:properties]['klass'] = 'Element'
  properties = params[:properties]
  properties.delete('flow') unless properties['flow'].nil?
  properties.delete('select_options') unless properties['select_options'].nil?
  attributes = {
    name: params[:name],
    element_klass_id: klass[:id],
    uuid: uuid,
    klass_uuid: klass[:uuid],
    properties: properties,
    properties_release: params[:properties_release],
    created_by: current_user.id,
  }
  element = Element.new(attributes)

  if params[:collection_id]
    collection = current_user.collections.find(params[:collection_id])
    element.collections << collection
  end

  all_coll = Collection.get_all_collection_for_user(current_user.id)
  element.collections << all_coll
  element.save!

  element.properties = update_sample_association(element, params[:properties], current_user)
  element.container = update_datamodel(params[:container], current_user)
  element.save!
  element.save_segments(segments: params[:segments], current_user_id: current_user.id)
  element
end

#create_element_klass(current_user, params, version) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/labimotion/helpers/element_helpers.rb', line 18

def create_element_klass(current_user, params, version)
  uuid = SecureRandom.uuid
  template = { uuid: uuid, layers: {}, select_options: {} }
  attributes = declared(params, include_missing: false)
  attributes[:properties_template]['uuid'] = uuid if attributes[:properties_template].present?
  attributes[:properties_template] = template unless attributes[:properties_template].present?
  attributes[:properties_template]['eln'] = version if attributes[:properties_template].present?
  attributes[:properties_template]['klass'] = 'ElementKlass' if attributes[:properties_template].present?
  attributes[:is_active] = false
  attributes[:uuid] = uuid
  attributes[:released_at] = DateTime.now
  attributes[:properties_release] = attributes[:properties_template]
  attributes[:created_by] = current_user.id

  new_klass = ElementKlass.create!(attributes)
  new_klass.reload
  new_klass.create_klasses_revision(current_user.id)
  klass_names_file = Rails.root.join('config', 'klasses.json')
  klasses = ElementKlass.where(is_active: true)&.pluck(:name) || []
  File.write(klass_names_file, klasses)
end

#element_revisions(params) ⇒ Object



163
164
165
166
167
# File 'lib/labimotion/helpers/element_helpers.rb', line 163

def element_revisions(params)
  klass = Element.find(params[:id])
  list = klass.elements_revisions unless klass.nil?
  list&.sort_by(&:created_at)&.reverse
end

#klass_list(is_generic_only) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/labimotion/helpers/element_helpers.rb', line 10

def klass_list(is_generic_only)
  if is_generic_only == true
    ElementKlass.where(is_active: true, is_generic: true).order('place') || []
  else
    ElementKlass.where(is_active: true).order('place') || []
  end
end

#list_user_elements(scope, params) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/labimotion/helpers/element_helpers.rb', line 169

def list_user_elements(scope, params)
  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 = ElementKlass.find_by(name: params[:el_type])
    allowed_fields = element_klass.properties_release.dig('layers', layer, 'fields')&.pluck('field') || []

    if field.in?(allowed_fields)
      query = ActiveRecord::Base.sanitize_sql(
        [
          "LEFT JOIN LATERAL(
            SELECT field->'value' AS value
            FROM jsonb_array_elements(properties->'layers'->:layer->'fields') a(field)
            WHERE field->>'field' = :field
          ) a ON true",
          { layer: layer, field: field },
        ],
      )
      scope = scope.joins(query).order('value ASC NULLS FIRST')
    else
      scope = scope.order(updated_at: :desc)
    end
  else
    scope = scope.order(updated_at: :desc)
  end

  scope = scope.created_time_from(Time.at(from)) if from && by_created_at
  scope = scope.created_time_to(Time.at(to) + 1.day) if to && by_created_at
  scope = scope.updated_time_from(Time.at(from)) if from && !by_created_at
  scope = scope.updated_time_to(Time.at(to) + 1.day) if to && !by_created_at
  scope
end

#update_element_by_id(current_user, params, version) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/labimotion/helpers/element_helpers.rb', line 94

def update_element_by_id(current_user, params, version)
  element = Element.find(params[:id])
  update_datamodel(params[:container], current_user)
  properties = update_sample_association(element, params[:properties], current_user)
  params.delete(:container)
  params.delete(:properties)

  attributes = declared(params.except(:segments), include_missing: false)
  properties['eln'] = version if properties['eln'] != version
  if element.klass_uuid != properties['klass_uuid'] || element.properties != properties || element.name != params[:name]
    properties['klass'] = 'Element'
    uuid = SecureRandom.uuid
    properties['uuid'] = uuid

    properties.delete('flow') unless properties['flow'].nil?
    properties.delete('select_options') unless properties['select_options'].nil?

    attributes['properties'] = properties
    attributes['properties']['uuid'] = uuid
    attributes['uuid'] = uuid
    attributes['klass_uuid'] = properties['klass_uuid']

    element.update(attributes)
  end
  element.save_segments(segments: params[:segments], current_user_id: current_user.id)
  element
end

#update_element_klass(params) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/labimotion/helpers/element_helpers.rb', line 40

def update_element_klass(params)
  place = params[:place] || 100
  begin
    place = place.to_i if place.present? && place.to_i == place.to_f
  rescue StandardError
    place = 100
  end
  klass = ElementKlass.find(params[:id])
  klass.label = params[:label] if params[:label].present?
  klass.klass_prefix = params[:klass_prefix] if params[:klass_prefix].present?
  klass.icon_name = params[:icon_name] if params[:icon_name].present?
  klass.desc = params[:desc] if params[:desc].present?
  klass.place = place
  klass.save!
  klass
end

#upload_generics_files(current_user, params, is_repo = false) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/labimotion/helpers/element_helpers.rb', line 122

def upload_generics_files(current_user, params, is_repo = false)
  attach_ary = []
  att_ary = create_uploads(
    'Element',
    params[:att_id],
    params[:elfiles],
    params[:elInfo],
    current_user.id,
  ) if params[:elfiles].present? && params[:elInfo].present?

  (attach_ary << att_ary).flatten! unless att_ary&.empty?

  att_ary = create_uploads(
    'Segment',
    params[:att_id],
    params[:sefiles],
    params[:seInfo],
    current_user.id
  ) if params[:sefiles].present? && params[:seInfo].present?

  (attach_ary << att_ary).flatten! unless att_ary&.empty?

  if params[:attfiles].present? || params[:delfiles].present? then
    att_ary = create_attachments(
      params[:attfiles],
      params[:delfiles],
      params[:att_type],
      params[:att_id],
      params[:attfilesIdentifier],
      current_user.id
    )
  end
  (attach_ary << att_ary).flatten! unless att_ary&.empty?

  if is_repo == true
    TransferThumbnailToPublicJob.set(queue: "transfer_thumbnail_to_public_#{current_user.id}").perform_now(attach_ary) unless attach_ary.empty?
    TransferFileFromTmpJob.set(queue: "transfer_file_from_tmp_#{current_user.id}").perform_now(attach_ary) unless attach_ary.empty?
  end
  true
end