Class: Labimotion::ExportElementBak1

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

Instance Method Summary collapse

Constructor Details

#initialize(current_user, element, export_format) ⇒ ExportElementBak1

Returns a new instance of ExportElementBak1.



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

def initialize(current_user, element, export_format)
  @current_user = current_user
  @element = element
  @properties = element.properties
  @export_format = export_format
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

Instance Method Details

#build_field(layer, field) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/labimotion/libs/export_element_bak1.rb', line 18

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

  case field['type']
  when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
    field_obj[:value] = field['value']['el_label']
  else
    field_obj[:value] = field['value']
  end
  field_obj
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

#build_fields(layer, fields) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/labimotion/libs/export_element_bak1.rb', line 36

def build_fields(layer, 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_layersObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/labimotion/libs/export_element_bak1.rb', line 47

def build_layers
  objs = []
  @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
    layer = @properties[Labimotion::Prop::LAYERS][key] || {}
    fields = layer[Labimotion::Prop::FIELDS] || []
    field_objs = build_fields(layer, fields)

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

#html_sampleObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/labimotion/libs/export_element_bak1.rb', line 68

def html_sample
  html_body = <<-HTML.strip
  <p style="text-align: right; background-color: #FFFF00">
      Right aligned content with a yellow background color.
  </p>
  <div>
      <span style="color: #123456">Inline styles</span> are possible as well
  </div>
  <table>
    <tr>
      <th>Company</th>
      <th>Contact</th>
      <th>Country</th>
    </tr>
    <tr>
      <td>Alfreds Futterkiste</td>
      <td>Maria Anders</td>
      <td>Germany</td>
    </tr>
  </table>

  HTML
  html_body
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end

#to_docxObject



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

def to_docx
  location = Rails.root.join('lib', 'template', 'Labimotion.docx')
  File.exist?(location)
  template = Sablon.template(location)
  layers = build_layers
byebug
  html_body  = html_sample
  html_content = Sablon.content(:html, html_body)
  context = {
    name: @element.name,
    short_label: @element.short_label,
    layers: layers,
    html_sample: html_content
  }
  output = "Labimotion1.docx"
  template.render_to_file File.expand_path(output), context
  File.read(output)
rescue StandardError => e
  Labimotion.log_exception(e)
  byebug
end