Class: Labimotion::SampleAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/libs/sample_association.rb

Class Method Summary collapse

Class Method Details

.build_sample(sid, cols, current_user, cr_opt) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/labimotion/libs/sample_association.rb', line 5

def self.build_sample(sid, cols, current_user, cr_opt)
  parent_sample = Sample.find(sid)
  case cr_opt
  when 0
    subsample = parent_sample
    collections = Collection.where(id: cols).where.not(id: subsample.collections.pluck(:id))
    subsample.collections << collections unless collections.empty?
  when 1
    subsample = parent_sample.create_subsample(current_user, cols, true)
  when 2
    subsample = parent_sample.dup
    subsample.parent = nil
    collections = (Collection.where(id: cols) | Collection.where(user_id: current_user.id, label: 'All', is_locked: true))
    subsample.collections << collections
    subsample.container = Container.create_root_container
  else
    return nil
  end

  return nil if subsample.nil?

  subsample.save!
  subsample.reload
  subsample
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  nil
end

.build_table_sample(field_tables, current_user, element = nil) ⇒ Object



34
35
36
37
38
39
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
# File 'lib/labimotion/libs/sample_association.rb', line 34

def self.build_table_sample(field_tables, current_user, element = nil)
  sds = []
  field_tables.each do |field|
    next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?

    field_table_samples = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
    next unless field_table_samples.present?

    col_ids = field_table_samples.map { |x| x.values[0] }
    col_ids.each do |col_id|
      field['sub_values'].each do |sub_value|
        next unless sub_value[col_id].present? && sub_value[col_id]['value'].present? && sub_value[col_id]['value']['el_id'].present?

        svalue = sub_value[col_id]['value']
        sid = svalue['el_id']
        next unless sid.present?

        sds << sid unless svalue['is_new']
        next unless svalue['is_new']

        cr_opt = svalue['cr_opt']
        next if element.nil?

        subsample = Labimotion::SampleAssociation.build_sample(sid, element.collections, current_user, cr_opt) unless sid.nil? || cr_opt.nil?
        next if subsample.nil?

        sds << subsample.id
        sub_value[col_id]['value']['el_id'] = subsample.id
        sub_value[col_id]['value']['is_new'] = false
        Labimotion::ElementsSample.find_or_create_by(element_id: element.id, sample_id: subsample.id)
      end
    end
  end
  sds
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  []
end

.update_sample_association(properties, current_user, element = nil) ⇒ Object



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
# File 'lib/labimotion/libs/sample_association.rb', line 73

def self.update_sample_association(properties, current_user, element = nil)
  sds = []
  els = []
  properties[Labimotion::Prop::LAYERS].keys.each do |key|
    layer = properties[Labimotion::Prop::LAYERS][key]
    field_samples = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
    field_samples.each do |field|
      idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
      sid = field.dig('value', 'el_id')
      next if sid.blank?

      sds << sid unless properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value', 'is_new') == true
      next unless properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value', 'is_new') == true

      cr_opt = field.dig('value', 'cr_opt')
      subsample = Labimotion::SampleAssociation.build_sample(sid, element&.collections, current_user, cr_opt) unless sid.nil? || cr_opt.nil?
      next if subsample.nil?

      sds << subsample.id
      properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_id'] = subsample.id
      properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = subsample.short_label
      properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = subsample.short_label
      properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['is_new'] = false
      Labimotion::ElementsSample.find_or_create_by(element_id: element.id, sample_id: subsample.id) if element.present?
    end
    field_tables = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
    sds << Labimotion::SampleAssociation.build_table_sample(field_tables, current_user, element) if element.present?
    field_elements = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_ELEMENT }
    field_elements.each do |field|
      idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
      sid = field.dig('value', 'el_id')
      next if element.nil? || sid.blank? || sid == element.id

      el = Labimotion::Element.find_by(id: sid)
      next if el.nil?

      Labimotion::ElementsElement.find_or_create_by(parent_id: element.id, element_id: el.id)
      els << el.id
    end

  end
  if element.present?
    es_list = Labimotion::ElementsSample.where(element_id: element.id).where.not(sample_id: sds)
    ee_list = Labimotion::ElementsElement.where(parent_id: element.id).where.not(element_id: els&.flatten)
    es_list.destroy_all if es_list.present?
    ee_list.destroy_all if ee_list.present?
  end
  properties
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
end