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



55
56
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
# File 'lib/labimotion/models/concerns/segmentable.rb', line 55

def save_segments(**args) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
  args_segments = args[:segments] || []

  current_user = User.find_by(id: args[:current_user_id])
  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])
    # props = Labimotion::VocabularyHandler.update_vocabularies(props, current_user, self)
    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'], metadata: seg['metadata'] || {})
      # 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, metadata: seg['metadata'] || {})
    # segments.push(segment)
  end

  self.reload
  touch_vocabulary(current_user)
  self.reload
  segments
end

#touch_analyses_properties(current_user) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/labimotion/models/concerns/segmentable.rb', line 98

def touch_analyses_properties(current_user)
  analyses.each do |analysis|
    analysis.children.each do |child|
      dataset = child.dataset
      next if dataset.nil?

      touch_properties_for_object(dataset, current_user)
    end
  end
end

#touch_element_properties(current_user) ⇒ Object



88
89
90
# File 'lib/labimotion/models/concerns/segmentable.rb', line 88

def touch_element_properties(current_user)
  touch_properties_for_object(self, current_user)
end

#touch_properties_for_object(object, current_user) ⇒ Object

NOTE: Update bypassing validations, callbacks, and timestamp updates



110
111
112
113
114
# File 'lib/labimotion/models/concerns/segmentable.rb', line 110

def touch_properties_for_object(object, current_user)
  props_dup = object.properties.deep_dup
  Labimotion::VocabularyHandler.update_vocabularies(props_dup, current_user, self)
  object.update_column(:properties, props_dup) if props_dup != object.properties
end

#touch_segments_properties(current_user) ⇒ Object



92
93
94
95
96
# File 'lib/labimotion/models/concerns/segmentable.rb', line 92

def touch_segments_properties(current_user)
  segments.each do |segment|
    touch_properties_for_object(segment, current_user)
  end
end

#touch_vocabulary(current_user) ⇒ Object



49
50
51
52
53
# File 'lib/labimotion/models/concerns/segmentable.rb', line 49

def touch_vocabulary(current_user)
  touch_element_properties(current_user) if instance_of?(::Labimotion::Element)
  touch_segments_properties(current_user)
  touch_analyses_properties(current_user)
end