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

#copy_segments(**args) ⇒ Object



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
36
37
38
# File 'lib/labimotion/models/concerns/segmentable.rb', line 11

def copy_segments(**args)
  return if args[:segments].nil?

  segments = save_segments(segments: args[:segments], current_user_id: args[:current_user_id])
  segments.each do |segment|
    properties = segment.properties
    properties['layers'].keys.each do |key|
      layer = properties['layers'][key]
      field_uploads = layer['fields'].select { |ss| ss['type'] == 'upload' }
      field_uploads&.each do |upload|
        idx = properties['layers'][key]['fields'].index(upload)
        files = upload["value"] && upload["value"]["files"]
        files&.each_with_index do |fi, fdx|
          aid = properties['layers'][key]['fields'][idx]['value']['files'][fdx]['aid']
          unless aid.nil?
            copied_att = Attachment.find(aid)&.copy(attachable_type: 'SegmentProps', attachable_id: segment.id, transferred: true)
            unless copied_att.nil?
              copied_att.save!
              properties['layers'][key]['fields'][idx]['value']['files'][fdx]['aid'] = copied_att.id
              properties['layers'][key]['fields'][idx]['value']['files'][fdx]['uid'] = copied_att.identifier
            end
          end
        end
      end
    end
    segment.update!(properties: properties)
  end
end

#save_segments(**args) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/labimotion/models/concerns/segmentable.rb', line 40

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

  segments = []
  args[:segments].each do |seg|
    klass = Labimotion::SegmentKlass.find_by(id: seg['segment_klass_id'])
    uuid = SecureRandom.uuid
    props = seg['properties']
    props['eln'] = Chemotion::Application.config.version
    props['labimotion'] = Labimotion::VERSION
    segment = Labimotion::Segment.find_by(element_type: self.class.name.split('::').last, 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['labimotion'] = Labimotion::VERSION
      props['klass'] = 'Segment'

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

    props['uuid'] = uuid
    props['klass_uuid'] = klass.uuid
    props['eln'] = Chemotion::Application.config.version
    props['labimotion'] = Labimotion::VERSION
    props['klass'] = 'Segment'

    segment = Labimotion::Segment.create!(properties_release: klass.properties_release, segment_klass_id: seg['segment_klass_id'], element_type: self.class.name.split('::').last, element_id: self.id, properties: props, created_by: args[:current_user_id], uuid: uuid, klass_uuid: klass.uuid)
    segments.push(segment)
  end
  segments
end