Class: Labimotion::ExportElement

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

Instance Method Summary collapse

Constructor Details

#initialize(current_user, element, export_format) ⇒ ExportElement

Returns a new instance of ExportElement.



8
9
10
11
12
13
14
15
16
17
# File 'lib/labimotion/libs/export_element1.rb', line 8

def initialize(current_user, element, export_format)
  @current_user = current_user
  @element = element
  @properties = element.properties
  @options = element.properties_release[Labimotion::Prop::SEL_OPTIONS]
  @export_format = export_format
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

Instance Method Details

#build_field(layer, field) ⇒ Object



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
75
76
77
78
79
80
81
82
83
84
# File 'lib/labimotion/libs/export_element1.rb', line 44

def build_field(layer, field)
  field_obj = {}
  field_obj[:label] = field['label']
  field_obj[:field] = field['field']
  field_obj[:type] = field['type']

  field_obj.merge!(field.slice('value', 'value_system'))

  case field['type']
  when Labimotion::FieldType::DRAG_ELEMENT
    field_obj[:value] = (field['value'] && field['value']['el_label']) || ''
    field_obj[:obj] = field['value']  ### change to object
  when Labimotion::FieldType::DRAG_SAMPLE
    val = field.fetch('value', {})
    sample = Sample.find_by(id: val['el_id'])
    field_obj[:value] = val['el_label']
    field_obj[:has_structure] = true
    obj_os = OpenStruct.new(sample)
    field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj_os, format: 'png').generate
  when Labimotion::FieldType::DRAG_MOLECULE
    val = field.fetch('value', {})
    obj = Molecule.find_by(id: val['el_id'])
    field_obj[:value] = val['el_label']
    # field_obj[:has_structure] = true
    # field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj, format: 'png').generate
  when Labimotion::FieldType::SELECT
    field_obj[:value] = @options.fetch(field['option_layers'], nil)&.fetch('options', nil)&.find { |ss| ss['key'] == field['value'] }&.fetch('label', nil) || field['value']
  when Labimotion::FieldType::UPLOAD
    files = field.fetch('value', nil)&.fetch('files', [])
    val = files&.map { |file| "#{file['filename']} #{file['label']}" }&.join('\n')
    field_obj[:value] = val
  when Labimotion::FieldType::TABLE
    field_obj[:value] = 'this is a table'  ## to be handled
  when Labimotion::FieldType::INPUT_GROUP
    field_obj[:value] = 'this is a input group'  ## to be handled
  end
  field_obj
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

#build_field_html(layer, field, cols) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/labimotion/libs/export_element1.rb', line 121

def build_field_html(layer, field, cols)
  case field['type']
  when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
    val = (field['value'] && field['value']['el_label']) || ''
  when Labimotion::FieldType::UPLOAD
    val = (field['value'] && field['value']['files'] && field['value']['files'].first && field['value']['files'].first['filename'] ) || ''
  else
    val = field['value']
  end
  htd = field['hasOwnRow'] == true ? "<td colspan=#{cols}>" : '<td>'
  "#{htd}<b>#{field['label']}: </b><br />#{val}</td>"
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

#build_fields(layer) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/labimotion/libs/export_element1.rb', line 86

def build_fields(layer)
  fields = layer[Labimotion::Prop::FIELDS] || []
  field_objs = []
  fields.each do |field|
    field_obj = build_field(layer, field)
    field_objs.push(field_obj)
  end
  field_objs
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

#build_fields_html(layer) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/labimotion/libs/export_element1.rb', line 137

def build_fields_html(layer)
  fields = layer[Labimotion::Prop::FIELDS] || []
  field_objs = []
  cols = layer['cols'] || 0
  field_objs.push('<table style="width: 4000dxa"><tr>')
  fields.each do |field|
    if cols&.zero? || field['hasOwnRow'] == true
      field_objs.push('</tr><tr>')
      cols = layer['cols']
    end
    field_obj = build_field_html(layer, field, layer['cols'])
    field_objs.push(field_obj)
    cols -= 1
    cols = 0 if field['hasOwnRow'] == true
  end
  field_objs.push('</tr></table>')
  field_objs&.join("")&.gsub('<tr></tr>', '')
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

#build_layersObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/labimotion/libs/export_element1.rb', line 19

def build_layers
  objs = []
  @properties[Labimotion::Prop::LAYERS]&.keys&.sort_by { |key| @properties[Labimotion::Prop::LAYERS][key]['position'] }&.each do |key|
    layer = @properties[Labimotion::Prop::LAYERS][key] || {}

    ## Build fields html
    # field_objs = build_fields_html(layer) if layer[Labimotion::Prop::FIELDS]&.length&.positive?
    # field_html = Sablon.content(:html, field_objs) if field_objs.present?
    field_objs = build_fields(layer)

    layer_info = {
      label: layer['label'],
      layer: layer['layer'],
      cols: layer['cols'],
      timeRecord: layer['timeRecord'],
      fields: field_objs
    }
    objs.push(layer_info)
  end
  objs
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

#build_layers_htmlObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/labimotion/libs/export_element1.rb', line 160

def build_layers_html
  layer_html = []
  first_line = true
  @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
    layer_html.push('</tr></table>') if first_line == false
    layer = @properties[Labimotion::Prop::LAYERS][key] || {}
    fields = layer[Labimotion::Prop::FIELDS] || []
    layer_html.push("<h2><b>Layer:#{layer['label']}</b></h2>")
    layer_html.push('<table><tr>')
    cols = layer['cols']
    fields.each do |field|
      if (cols === 0)
        layer_html.push('</tr><tr>')
      end

      val = field['value'].is_a?(Hash) ? field['value']['el_label'] : field['value']
      layer_html.push("<td>#{field['label']}: <br />#{val}</td>")
      cols -= 1
    end
    first_line = false
  end
  layer_html.push('</tr></table>')
  byebug
  layer_html.join('')
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

#html_labimotionObject



189
190
191
192
193
194
195
196
197
198
# File 'lib/labimotion/libs/export_element1.rb', line 189

def html_labimotion
  layers_html = build_layers_html
  html_body = <<-HTML.strip
  #{layers_html}
  HTML
  html_body
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

#to_docxObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/labimotion/libs/export_element1.rb', line 99

def to_docx
  # location = Rails.root.join('lib', 'template', 'Labimotion.docx')
  location = Rails.root.join('lib', 'template', 'Labimotion_lines.docx')
  File.exist?(location)
  template = Sablon.template(location)
  layers = build_layers
  context = {
    name: @element.name,
    short_label: @element.short_label,
    date: Time.now.strftime('%d/%m/%Y'),
    author: @current_user.name,
    layers: layers
  }
  output = "Labimotion1.docx"
  template.render_to_file File.expand_path(output), context
  File.read(output)
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end