Module: Labimotion::ElementHelpers

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

Overview

ElementHelpers

Constant Summary

Constants included from CoverImageHelpers

CoverImageHelpers::COVER_THUMB_RESIZE, CoverImageHelpers::SVG_TRANSPARENT_RE

Instance Method Summary collapse

Methods included from CoverImageHelpers

#analysis_preview_attachment, #cover_image_attachment, #cover_image_body, #cover_image_entry_error!, #cover_image_payload, #cover_images_error!, #cover_rasterize, #cover_read_ext, #normalized_cover_image!, #pick_preview_attachment, #preferred_candidate, #prepare_element_metadata, #save_cover_images, #svg_cover_thumbnail, #validate_cover_images!

Instance Method Details

#ai_fill_analysis_attachments(analysis) ⇒ Object



287
288
289
290
291
# File 'lib/labimotion/helpers/element_helpers.rb', line 287

def ai_fill_analysis_attachments(analysis)
  analysis.children.where(container_type: 'dataset').flat_map do |dataset|
    Attachment.where(attachable_type: 'Container', attachable_id: dataset.id).to_a
  end
end

#ai_fill_analysis_text(element, container_id) ⇒ Object

The container id must be one of the element's own analysis containers. Then concatenate the extracted text of every attachment on that analysis's dataset children, each prefixed "### ", with the analysis name/description prepended as context. Cap at AiTemplate::MAX_FILES; skip unreadable files.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/labimotion/helpers/element_helpers.rb', line 232

def ai_fill_analysis_text(element, container_id)
  error!('404 Not Found', 404) if container_id.nil?
  analysis = element.analyses.detect { |c| c.id == container_id.to_i }
  error!('404 Not Found', 404) if analysis.nil?

  parts = []
  context = [analysis.name.presence, analysis.description.presence].compact.join('')
  parts << "Analysis: #{context}" if context.present?

  ai_fill_analysis_attachments(analysis).first(Labimotion::AiTemplate::MAX_FILES).each do |att|
    content = ai_fill_extract_attachment(att)
    next if content.to_s.strip.empty?

    parts << "### #{att.filename}\n#{content}"
  end
  parts.join("\n\n")
end

#ai_fill_attachment_text(element, attachment_id) ⇒ Object

The attachment must be one of the element's own attachments OR an attachment of one of the element's analysis DATASET containers. Only then is it read.



220
221
222
223
224
225
226
# File 'lib/labimotion/helpers/element_helpers.rb', line 220

def ai_fill_attachment_text(element, attachment_id)
  error!('404 Not Found', 404) if attachment_id.nil?
  error!('404 Not Found', 404) unless ai_fill_element_attachment_ids(element).include?(attachment_id.to_i)

  att = Attachment.find(attachment_id)
  Labimotion::FileExtractor.extract_bytes(att.filename, att.read_file)
end

#ai_fill_dataset_containers(element) ⇒ Object

Dataset containers under all of the element's analysis containers.



283
284
285
# File 'lib/labimotion/helpers/element_helpers.rb', line 283

def ai_fill_dataset_containers(element)
  element.analyses.flat_map { |analysis| analysis.children.where(container_type: 'dataset').to_a }
end

#ai_fill_element_attachment_ids(element) ⇒ Object

Ids of attachments this element may expose to the AI fill: its own attachments plus the attachments of its analysis dataset containers.



275
276
277
278
279
280
# File 'lib/labimotion/helpers/element_helpers.rb', line 275

def ai_fill_element_attachment_ids(element)
  own = element.attachments.pluck(:id)
  dataset_ids = ai_fill_dataset_containers(element).map(&:id)
  dataset_att = dataset_ids.empty? ? [] : Attachment.where(attachable_type: 'Container', attachable_id: dataset_ids).pluck(:id)
  (own + dataset_att).uniq
end

#ai_fill_element_data(params, current_user) ⇒ Object

AI auto-fill of a generic element instance's DATA VALUES from a document (an existing attachment, an analysis, or freshly uploaded files). Reads the source text, asks the LLM to extract values for the element's OWN template fields, validates them, and returns them for the client to merge into the working copy for human review. Nothing is persisted here.

Authorization: the caller must be able to READ the element, and the source must BELONG to that element (see the ownership checks below) — this endpoint reads files by id, so it must never read an Attachment/Container the user does not already have access to through this element.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/labimotion/helpers/element_helpers.rb', line 147

def ai_fill_element_data(params, current_user)
  element = Labimotion::Element.find(params[:element_id])
  error!('401 Unauthorized', 401) unless ElementPolicy.new(current_user, element).read?

  text = ai_fill_source_text(element, params)
  overrides = ai_user_overrides(current_user)
  ai = Labimotion::AiTemplate.fill(
    properties: ai_fill_properties(element),
    context_text: text,
    instructions: ai_fill_instructions(element),
    **overrides
  )
  { status: 'success', values: ai['values'], summary: ai['summary'], usage: ai['usage'], model: ai['model'] }
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  { status: 'error', message: e.message }
end

#ai_fill_extract_attachment(att) ⇒ Object



293
294
295
296
297
298
# File 'lib/labimotion/helpers/element_helpers.rb', line 293

def ai_fill_extract_attachment(att)
  Labimotion::FileExtractor.extract_bytes(att.filename, att.read_file)
rescue StandardError => e
  Labimotion.log_exception(e)
  nil
end

#ai_fill_instructions(element) ⇒ Object

The template's own auto-fill guidance, from Template settings in the designer. Read from the KLASS rather than the element's copied properties, so editing it takes effect on elements that already exist instead of only on new ones.



195
196
197
198
199
200
# File 'lib/labimotion/helpers/element_helpers.rb', line 195

def ai_fill_instructions(element)
  settings = element.element_klass&.settings
  return nil unless settings.is_a?(Hash)

  settings[Labimotion::Constants::KlassSetting::AI_FILL_PROMPT].presence
end

#ai_fill_pasted_text(text) ⇒ Object

Text pasted straight into the dialog. Capped like an extracted file so a very long paste cannot blow the model's context or the request budget — the same MAX_FILE_CHARS the file path is held to.



266
267
268
269
270
271
# File 'lib/labimotion/helpers/element_helpers.rb', line 266

def ai_fill_pasted_text(text)
  body = text.to_s.strip
  error!('400 Bad Request', 400) if body.empty?

  body[0, Labimotion::AiTemplate::MAX_FILE_CHARS].to_s
end

#ai_fill_properties(element) ⇒ Object

The element's properties, with the select_options its own fields point at.

An element instance never stores select_options: create and update both delete the key (see create_element/update_element below), because the options belong to the template, not to the instance. The fill prompt lists a select's allowed options so the model can pick one, and reads them out of the properties it is handed — so passing element.properties straight through offered the model an EMPTY list for every select and select-multi field. Nothing to choose from, so those fields came back unfilled, which reads as the AI having ignored them.

properties_release, not the klass's current properties_template: it is the release this element was actually built against, so its option keys are the ones the element's fields reference and the ones the form will accept back. Same source the UI and the exporter read for this element.



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/labimotion/helpers/element_helpers.rb', line 180

def ai_fill_properties(element)
  props = element.properties
  return props unless props.is_a?(Hash)
  return props if props['select_options'].present?

  release = element.properties_release
  options = release.is_a?(Hash) ? release['select_options'] : nil
  return props if options.blank?

  props.merge('select_options' => options)
end

#ai_fill_source_text(element, params) ⇒ Object

Resolve the requested source into plain text, enforcing ownership first.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/labimotion/helpers/element_helpers.rb', line 203

def ai_fill_source_text(element, params)
  case params[:source]
  when 'attachment'
    ai_fill_attachment_text(element, params[:attachment_id])
  when 'analysis'
    ai_fill_analysis_text(element, params[:container_id])
  when 'upload'
    ai_fill_upload_text(params[:files])
  when 'text'
    ai_fill_pasted_text(params[:text])
  else
    error!('400 Bad Request', 400)
  end
end

#ai_fill_upload_text(files) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/labimotion/helpers/element_helpers.rb', line 250

def ai_fill_upload_text(files)
  parts = Array(files).first(Labimotion::AiTemplate::MAX_FILES).map do |file|
    name = file[:filename] || file['filename']
    next if name.blank?

    content = Labimotion::FileExtractor.extract(name, file[:content_base64] || file['content_base64'])
    next if content.to_s.strip.empty?

    "### #{name}\n#{content}"
  end.compact
  parts.join("\n\n")
end

#attach_thumbnail(_attachments) ⇒ Object



626
627
628
629
630
631
632
633
634
635
636
# File 'lib/labimotion/helpers/element_helpers.rb', line 626

def attach_thumbnail(_attachments)
  attachments = _attachments&.map do |attachment|
    _att = Entities::AttachmentEntity.represent(attachment, serializable: true)
    _att[:thumbnail] = attachment.thumb ? Base64.encode64(attachment.read_thumbnail) : nil
    _att
  end
  attachments
rescue StandardError => e
  Labimotion.log_exception(e)
  attachments
end

#create_ai_element_klass(params, current_user) ⇒ Object

Create a new (inactive) element klass whose properties template is generated by an LLM from a user-provided name/label plus optional description, reference links and uploaded files. The admin reviews/edits the generated template in the designer and activates it (human-in-the-loop).



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
93
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/labimotion/helpers/element_helpers.rb', line 61

def create_ai_element_klass(params, current_user)
  name = params[:name]
  label = params[:label]
  if Labimotion::ElementKlass.find_by(name: name).present?
    return { status: 'error', message: "An element named [#{name}] already exists." }
  end

  if Array(params[:files]).size > Labimotion::AiTemplate::MAX_FILES
    return { status: 'error', message: "Too many files (max #{Labimotion::AiTemplate::MAX_FILES})." }
  end

  overrides = ai_user_overrides(current_user)
  ai = Labimotion::AiTemplate.generate(
    kind: 'element',
    subject: "#{params[:label]} (#{params[:name]})",
    desc: params[:desc],
    cols: params[:cols],
    references: params[:references],
    files: params[:files],
    **overrides
  )

  uuid = SecureRandom.uuid
  # property-base schema requires pkg, uuid, klass, layers, version, identifier.
  properties_template = {
    'uuid' => uuid,
    'klass' => 'ElementKlass',
    'pkg' => Labimotion::Utils.pkg(nil),
    'version' => '1.0.0',
    'identifier' => uuid,
    'layers' => ai['layers'],
    'select_options' => ai['select_options'],
    'metadata' => ai['metadata']
  }
  attributes = {
    'name' => name,
    'label' => label,
    'klass_prefix' => params[:klass_prefix],
    'icon_name' => params[:icon_name],
    'desc' => params[:desc].presence || ai['label'].presence,
    'is_active' => false,
    'uuid' => uuid,
    'released_at' => DateTime.now,
    'properties_template' => properties_template,
    'properties_release' => properties_template,
    'created_by' => current_user.id
  }

  # Same as the non-AI create beside it: the owner row shares the create's
  # transaction, because a klass committed without one is owner-less and
  # falls OPEN to the legacy designer-wide gate — every designer of the
  # family could then edit, release and delete it, while its creator holds
  # no special standing at all. seed_owner! rescues only RecordNotUnique and
  # RecordInvalid, so any other failure has to take the klass down with it;
  # this helper turns every StandardError into { status: 'error' }, and
  # without the transaction the caller would be told the create failed while
  # an unowned template silently persisted.
  new_klass = Labimotion::ElementKlass.transaction do
    Labimotion::ElementKlass.create!(attributes).tap do |klz|
      Labimotion::KlassShare.seed_owner!(klz, current_user.id)
    end
  end
  new_klass.reload
  new_klass.create_klasses_revision(current_user)
  klass_names_file = Labimotion::KLASSES_JSON # Rails.root.join('app/packs/klasses.json')
  klasses = Labimotion::ElementKlass.where(is_active: true)&.pluck(:name) || []
  File.write(klass_names_file, klasses)
  # `record` is the row itself. The background job that calls this needs the
  # template, not a sentence about it, to link the notification back to it.
  { status: 'success', record: new_klass,
    message: "The AI element template [#{label}] has been created as inactive. Review and activate it in the designer." }
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  { status: 'error', message: e.message }
end

#create_element(current_user, params) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/labimotion/helpers/element_helpers.rb', line 321

def create_element(current_user, params)
  klass = params[:element_klass] || {}
  uuid = SecureRandom.uuid
  params[:properties]['uuid'] = uuid
  params[:properties]['klass_uuid'] = klass[:uuid]
  params[:properties]['pkg'] = Labimotion::Utils.pkg(params[:properties]['pkg'])
  params[:properties]['klass'] = 'Element'
  params[:properties]['identifier'] = klass[:identifier]
  properties = params[:properties]
  properties.delete('flow') unless properties['flow'].nil?
  properties.delete('flowObject') unless properties['flowObject'].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],
    metadata: ({}, params[:metadata]),
    created_by: current_user.id
  }
  element = Labimotion::Element.new(attributes)

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

  if params[:collection_id] && params[:collection_id] != all_coll.id
    collection = current_user.collections.find(params[:collection_id])
    element.collections << collection
  end
  element.save!
  element.properties = update_sample_association(params[:properties], current_user, element)
  # element.properties = update_vocabularies(_properties, current_user, element)
  element.container = update_datamodel(params[:container], current_user)
  element.save!
  update_element_labels(element, params[:user_labels], current_user.id)
  element.save_segments(segments: params[:segments], current_user_id: current_user.id)
  element.save!
  element
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  raise e
end

#create_element_klass(current_user, params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/labimotion/helpers/element_helpers.rb', line 25

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

  # The owner row shares the create's transaction: a klass committed without it is
  # owner-less, which falls open to the legacy designer-wide gate.
  new_klass = Labimotion::ElementKlass.transaction do
    Labimotion::ElementKlass.create!(attributes).tap do |klz|
      Labimotion::KlassShare.seed_owner!(klz, current_user.id)
    end
  end
  new_klass.reload
  new_klass.create_klasses_revision(current_user)
  klass_names_file = Labimotion::KLASSES_JSON # Rails.root.join('app/packs/klasses.json')
  klasses = Labimotion::ElementKlass.where(is_active: true)&.pluck(:name) || []
  File.write(klass_names_file, klasses)
  klasses
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  raise e
end

#create_repo_klass(params, current_user, origin) ⇒ Object



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/labimotion/helpers/element_helpers.rb', line 609

def create_repo_klass(params, current_user, origin)
  response = Labimotion::TemplateHub.fetch_identifier('ElementKlass', params[:identifier], origin)
  attributes = response.slice('name', 'label', 'desc', 'icon_name', 'uuid', 'klass_prefix', 'is_generic', 'identifier', 'properties_release', 'version', 'metadata')
  attributes['properties_release']['identifier'] = attributes['identifier']
  attributes['properties_template'] = attributes['properties_release']
  attributes['place'] = ((Labimotion::ElementKlass.all.length * 10) || 0) + 10
  attributes['is_active'] = false
  attributes['updated_by'] = current_user.id
  attributes['sync_by'] = current_user.id
  attributes['sync_time'] = DateTime.now
  validate_klass(attributes)
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  # { error: e.message }
  raise e
end

#element_revisions(params) ⇒ Object



459
460
461
462
463
464
465
466
# File 'lib/labimotion/helpers/element_helpers.rb', line 459

def element_revisions(params)
  klass = Labimotion::Element.find(params[:id])
  list = klass.elements_revisions unless klass.nil?
  list&.order(created_at: :desc)&.limit(params[:limit])
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  raise e
end

#klass_list(is_generic_only) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/labimotion/helpers/element_helpers.rb', line 17

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

#list_serialized_elements(params, current_user) ⇒ Object



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/labimotion/helpers/element_helpers.rb', line 508

def list_serialized_elements(params, current_user)
  scope = Labimotion::Element.none

  if params[:collection_id]
    begin
      collection = Collection.accessible_for(current_user).find(params[:collection_id])
      scope = collection.elements
                        .joins(:element_klass)
                        .where(element_klasses: { name: params[:el_type] })
                        .includes(:tag)
    rescue ActiveRecord::RecordNotFound
      Labimotion::Element.none
    end
  else
    # All collection of current_user
    scope = Labimotion::Element.for_user(current_user.id)
  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') || []

    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.elements_created_time_from(Time.at(from)) if from && by_created_at
  scope = scope.elements_created_time_to(Time.at(to) + 1.day) if to && by_created_at
  scope = scope.elements_updated_time_from(Time.at(from)) if from && !by_created_at
  scope = scope.elements_updated_time_to(Time.at(to) + 1.day) if to && !by_created_at
  scope = scope.by_user_label(params[:user_label]) if params[:user_label]
  scope
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  raise e
end

#list_user_elements(scope, params) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/labimotion/helpers/element_helpers.rb', line 468

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 = Labimotion::ElementKlass.find_by(name: params[:el_type])
    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(
        [
          "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
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  raise e
end

#split_elements(ui_state, current_user) ⇒ Object



410
411
412
413
414
415
416
417
418
# File 'lib/labimotion/helpers/element_helpers.rb', line 410

def split_elements(ui_state, current_user)
  col_id = ui_state[:currentCollectionId] || 0
  element_ids = Labimotion::Element.for_user(current_user.id).for_ui_state_with_collection(ui_state[:element], Labimotion::CollectionsElement, col_id)
  klass_id = Labimotion::ElementKlass.find_by(name: ui_state[:element][:name])&.id
  Labimotion::Element.where(id: element_ids, element_klass_id: klass_id).each do |element|
    element.split(current_user, col_id)
  end
  {}
end

#update_element_by_id(current_user, params) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/labimotion/helpers/element_helpers.rb', line 366

def update_element_by_id(current_user, params)
  element = Labimotion::Element.find(params[:id])
  # Validated up front, before the container/sample-association writes run:
  # a rejected cover selection must fail the request whole, not leave a
  # half-applied datamodel behind. error! throws rather than raises, so it
  # escapes this method's rescue-and-log and reaches the client as a 422.
   = (element., params[:metadata])
  update_datamodel(params[:container], current_user)
  properties = update_sample_association(params[:properties], current_user, element)
  params.delete(:container)
  params.delete(:properties)
  update_element_labels(element, params[:user_labels], current_user.id)
  params.delete(:user_labels)
  attributes = declared(params.except(:segments), include_missing: false)
  properties['pkg'] = Labimotion::Utils.pkg(properties['pkg'])
  if element.klass_uuid != properties['klass_uuid'] || element.properties != properties || element.name != params[:name] || element. != 
    properties['klass'] = 'Element'
    uuid = SecureRandom.uuid
    properties['uuid'] = uuid

    properties.delete('flow') unless properties['flow'].nil?
    properties.delete('flowObject') unless properties['flowObject'].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']
    attributes['metadata'] = 
    attributes['updated_at'] = Time.current
    element.update_columns(attributes)
  end
  # element.save_segments(segments: params[:segments], current_user_id: current_user.id)
  element.reload
  element.save_segments(segments: params[:segments], current_user_id: current_user.id)
  element.reload
  # element.properties = update_vocabularies(element.properties, current_user, element)
  ## element.user_for_revision = current_user
  element.save!
  element
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  raise e
end

#update_element_klass(current_user, params) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/labimotion/helpers/element_helpers.rb', line 300

def update_element_klass(current_user, 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 = Labimotion::ElementKlass.find(params[:id])
  authorize_klass!(klass, :write)
  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
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  raise e
end

#upload_generics_files(current_user, params) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/labimotion/helpers/element_helpers.rb', line 420

def upload_generics_files(current_user, params)
  attach_ary = []
  att_ary = create_uploads(
    Labimotion::Prop::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(
    Labimotion::Prop::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],
      "Labimotion::#{params[:att_type]}",
      params[:att_id],
      params[:attfilesIdentifier],
      current_user.id
    )
  end
  (attach_ary << att_ary).flatten! unless att_ary&.empty?
  true
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  false
end

#validate_klass(attributes) ⇒ Object



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/labimotion/helpers/element_helpers.rb', line 566

def validate_klass(attributes)
  # Guarded on presence like the segment path: a blank identifier must mean "create",
  # never `find_by(identifier: nil)` — that would match an arbitrary legacy row whose
  # identifier is NULL and upgrade an unrelated template.
  element_klass = Labimotion::ElementKlass.find_by(identifier: attributes['identifier']) if attributes['identifier'].present?
  if element_klass.present?
    if element_klass['uuid'] == attributes['uuid'] && element_klass['version'] == attributes['version']
      return { status: 'success', message: "This element: #{attributes['name']} has the latest version!" }
    else
      # Upgrading an existing template bumps its version and cuts a revision — publishing,
      # which is what :release means, so it is owner-only. The gate has to live here, not
      # in after_validation: which branch applies is unknown until this identifier lookup
      # has run. error! throws rather than raises, so it escapes this method's rescue.
      authorize_klass!(element_klass, :release)
      # And it must not reassign authorship. `upload_klass` merges
      # `created_by: current_user.id` into the attributes unconditionally
      # (generic_element_api.rb), so without this the importer silently becomes the author
      # of a template somebody else wrote — and that column is what the Owner column shows
      # and what per-template ownership is seeded from.
      element_klass.update!(attributes.except('created_by', :created_by))
      element_klass.create_klasses_revision(current_user)
      return { status: 'success', message: "This element: [#{attributes['name']}] has been upgraded to the version: #{attributes['version']}!" }
    end
  else
    exist_klass = Labimotion::ElementKlass.find_by(name: attributes['name'])
    if exist_klass.present?
      return { status: 'error', message: "The name [#{attributes['name']}] is already in use." }
    else
      attributes['created_by'] = current_user.id
      element_klass = Labimotion::ElementKlass.transaction do
        Labimotion::ElementKlass.create!(attributes).tap do |klz|
          Labimotion::KlassShare.seed_owner!(klz, current_user.id)
        end
      end
      element_klass.create_klasses_revision(current_user)
      return { status: 'success', message: "The element: #{attributes['name']} has been created using version: #{attributes['version']}!" }
    end
  end
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  return { status: 'error', message: e.message }
end