Module: Labimotion::SampleAssociationHelpers

Extended by:
Grape::API::Helpers
Defined in:
lib/labimotion/helpers/sample_association_helpers.rb

Overview

Helper for associated sample

Instance Method Summary collapse

Instance Method Details

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



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
33
34
35
36
# File 'lib/labimotion/helpers/sample_association_helpers.rb', line 8

def 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(element, field_tables) ⇒ Object



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
72
73
74
# File 'lib/labimotion/helpers/sample_association_helpers.rb', line 38

def build_table_sample(element, field_tables)
  sds = []
  field_tables.each do |field|
    next unless field['sub_values'].present? && field['sub_fields'].present?

    field_table_samples = field['sub_fields'].select { |ss| ss['type'] == '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']

        subsample = 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(element, properties, current_user) ⇒ Object



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
124
# File 'lib/labimotion/helpers/sample_association_helpers.rb', line 76

def update_sample_association(element, properties, current_user)
  sds = []
  els = []
  properties['layers'].keys.each do |key|
    layer = properties['layers'][key]
    field_samples = layer['fields'].select { |ss| ss['type'] == 'drag_sample' }
    field_samples.each do |field|
      idx = properties['layers'][key]['fields'].index(field)
      sid = field.dig('value', 'el_id')
      next if sid.blank?

      sds << sid unless properties.dig('layers', key, 'fields', idx, 'value', 'is_new') == true
      next unless properties.dig('layers', key, 'fields', idx, 'value', 'is_new') == true

      cr_opt = field.dig('value', 'cr_opt')

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

      sds << subsample.id
      properties['layers'][key]['fields'][idx]['value']['el_id'] = subsample.id
      properties['layers'][key]['fields'][idx]['value']['el_label'] = subsample.short_label
      properties['layers'][key]['fields'][idx]['value']['el_tip'] = subsample.short_label
      properties['layers'][key]['fields'][idx]['value']['is_new'] = false
      Labimotion::ElementsSample.find_or_create_by(element_id: element.id, sample_id: subsample.id)
    end
    field_tables = properties['layers'][key]['fields'].select { |ss| ss['type'] == 'table' }
    sds << build_table_sample(element, field_tables)

    field_elements = layer['fields'].select { |ss| ss['type'] == 'drag_element' }
    field_elements.each do |field|
      idx = properties['layers'][key]['fields'].index(field)
      sid = field.dig('value', 'el_id')
      next if 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
  Labimotion::ElementsSample.where(element_id: element.id).where.not(sample_id: sds)&.destroy_all
  Labimotion::ElementsElement.where(parent_id: element.id).where.not(element_id: els&.flatten)&.destroy_all
  properties
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
end