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



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
39
40
41
42
43
44
45
46
47
# File 'lib/labimotion/models/concerns/segmentable.rb', line 14

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[Labimotion::Prop::LAYERS].keys.each do |key|
      layer = properties[Labimotion::Prop::LAYERS][key]
      field_uploads = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::UPLOAD }
      field_uploads&.each do |upload|
        idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(upload)
        files = upload["value"] && upload["value"]["files"]
        files&.each_with_index do |fi, fdx|
          aid = files[fdx]['aid']
          uid = files[fdx]['uid']
          next if aid.nil?

          att = Attachment.find_by(id: aid)
          att = Attachment.find_by(identifier: uid) if att.nil?
          copied_att = Labimotion::AttachmentHandler.copy(att, segment.id, Labimotion::Prop::SEGMENTPROPS, args[:current_user_id])

          if copied_att.nil?
            properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['files'].delete_at(fdx)
          else
            copied_att.save!
            properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['files'][fdx]['aid'] = copied_att.id
            properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['files'][fdx]['uid'] = copied_att.identifier
          end
        end
      end
    end
    segment.update!(properties: properties)
  end
end

#save_segments(**args) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/labimotion/models/concerns/segmentable.rb', line 49

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['pkg'] = Labimotion::Utils.pkg(props['pkg'])
    props['identifier'] = klass.identifier if klass.identifier.present?
    props['uuid'] = uuid
    props['klass'] = 'Segment'
    props = Labimotion::SampleAssociation.update_sample_association(props, args[:current_user_id])
    segment = Labimotion::Segment.where(element_type: self.class.name, element_id: self.id, segment_klass_id: seg['segment_klass_id']).order(id: :desc).first
    if segment.present? && (segment.klass_uuid != props['klass_uuid'] || segment.properties != props)
      segment.update!(properties_release: klass.properties_release, properties: props, uuid: uuid, klass_uuid: props['klass_uuid'])
      segments.push(segment)
      Labimotion::Segment.where(element_type: self.class.name, element_id: self.id, segment_klass_id: seg['segment_klass_id']).where.not(id: segment.id).destroy_all
    end
    next if segment.present?

    props['klass_uuid'] = klass.uuid
    segment = Labimotion::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)
    segments.push(segment)
  end
  segments
end