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_element(field_tables, current_user, element = nil) ⇒ Object

Link the generic elements referenced by drag_element table columns to the owning element, mirroring what layer-level drag_element fields do below.



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

def self.build_table_element(field_tables, current_user, element = nil)
  els = []
  return els if element.nil?

  field_tables.each do |field|
    next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?

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

    col_ids = field_table_elements.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 if sid == element.id

        # drag_element may link a standard element (reaction, cell_line, ...);
        # el_id is then NOT a Labimotion::Element id, so an ElementsElement row
        # would associate an unrelated generic element that shares the id.
        next if Labimotion::LinkedElement::MODELS.key?(svalue['el_klass'].to_s)

        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
  end
  els
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  []
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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/labimotion/libs/sample_association.rb', line 113

def self.update_sample_association(properties, current_user, element = nil)
  # Every element and segment properties save funnels through here, which
  # makes it the one place guaranteed to see the payload before it is stored.
  Labimotion::LinkedElement.strip_defaults(properties)
  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)
      return if field.is_a?(String) || properties.is_a?(String)
      next unless field['value'].is_a?(Hash)

      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 }
    if element.present?
      sds << Labimotion::SampleAssociation.build_table_sample(field_tables, current_user, element)
      els << Labimotion::SampleAssociation.build_table_element(field_tables, current_user, element)
    end
    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)
      return if field.is_a?(String) || properties.is_a?(String)
      next unless field['value'].is_a?(Hash)

      sid = field.dig('value', 'el_id')
      next if element.nil? || sid.blank? || sid == element.id

      # drag_element may link a standard element (reaction, cell_line, ...);
      # el_id is then NOT a Labimotion::Element id, so an ElementsElement row
      # would associate an unrelated generic element that shares the id.
      next if Labimotion::LinkedElement::MODELS.key?(field.dig('value', 'el_klass').to_s)

      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?
    sds = sds.flatten.uniq
    els = els.flatten.uniq
    es_list = sds.present? ? Labimotion::ElementsSample.where(element_id: element.id).where.not(sample_id: sds) : Labimotion::ElementsSample.where(element_id: element.id)
    ee_list = els.present? ? Labimotion::ElementsElement.where(parent_id: element.id).where.not(element_id: els) : Labimotion::ElementsElement.where(parent_id: element.id)
    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