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
124
125
126
127
128
|
# 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)
return if field.is_a?(String) || properties.is_a?(String)
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)
next if field['value'].is_a?(String)
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?
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
|