Module: Labimotion::Segmentable

Extended by:
ActiveSupport::Concern
Included in:
Element
Defined in:
lib/labimotion/models/concerns/segmentable.rb

Overview

Segmentable concern

Instance Method Summary collapse

Instance Method Details

#save_segments(**args) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/labimotion/models/concerns/segmentable.rb', line 11

def save_segments(**args) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
  return if args[:segments].nil?

  args[:segments].each do |seg|
    klass = SegmentKlass.find_by(id: seg['segment_klass_id'])
    uuid = SecureRandom.uuid
    props = seg['properties']
    props['eln'] = Chemotion::Application.config.version if props['eln'] != Chemotion::Application.config.version
    segment = Segment.find_by(element_type: self.class.name, element_id: self.id, segment_klass_id: seg['segment_klass_id'])
    if segment.present? && (segment.klass_uuid != props['klass_uuid'] || segment.properties != props)
      props['uuid'] = uuid
      props['eln'] = Chemotion::Application.config.version
      props['klass'] = 'Segment'

      segment.update!(properties_release: klass.properties_release, properties: props, uuid: uuid, klass_uuid: props['klass_uuid'])
    end
    next if segment.present?

    props['uuid'] = uuid
    props['klass_uuid'] = klass.uuid
    props['eln'] = Chemotion::Application.config.version
    props['klass'] = 'Segment'
    Segment.create!(properties_release: klass.properties_release, segment_klass_id: seg['segment_klass_id'], element_type: self.class.name, element_id: self.id, properties: props, created_by: args[:current_user_id], uuid: uuid, klass_uuid: klass.uuid)
  end
end