Module: Labimotion::Helpers::GenericHelpers

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

Overview

Generic Helpers

Instance Method Summary collapse

Instance Method Details

#authenticate_admin!(type) ⇒ Object



10
11
12
# File 'lib/labimotion/helpers/generic_helpers.rb', line 10

def authenticate_admin!(type)
  error!('401 Unauthorized', 401) unless current_user.generic_admin[type]
end

#create_attachments(files, del_files, type, id, user_id) ⇒ Object



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/helpers/generic_helpers.rb', line 100

def create_attachments(files, del_files, type, id, user_id)
  attach_ary = []
  (files || []).each do |file|
    next unless (tempfile = file[:tempfile])

    a = Attachment.new(
      bucket: file[:container_id],
      filename: file[:filename],
      file_path: file[:tempfile],
      created_by: user_id,
      created_for: user_id,
      content_type: file[:type],
      attachable_type: type,
      attachable_id: id,
    )
    begin
      a.save!
      attach_ary.push(a.id)
    ensure
      tempfile.close
      tempfile.unlink
    end
  end
  unless (del_files || []).empty?
    Attachment.where('id IN (?) AND attachable_type = (?)', del_files.map!(&:to_i),
                    type).update_all(attachable_id: nil)
  end
  attach_ary
end

#create_uploads(type, id, files, param_info, user_id) ⇒ Object



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/labimotion/helpers/generic_helpers.rb', line 52

def create_uploads(type, id, files, param_info, user_id)
  return if files.nil? || param_info.nil? || files.empty? || param_info.empty?

  attach_ary = []
  map_info = JSON.parse(param_info)
  map_info&.keys&.each do |key|
    next if map_info[key]['files'].empty?

    if type == 'Segment'
      element = Segment.find_by(element_id: id, segment_klass_id: key)
    elsif type == 'Element'
      element = Element.find_by(id: id)
    end
    next if element.nil?

    uploads = fetch_properties_uploads(element.properties)

    map_info[key]['files'].each do |fobj|
      file = (files || []).select { |ff| ff['filename'] == fobj['uid'] }&.first
      pa = uploads.select { |ss| ss[:uid] == file[:filename] }&.first || nil
      next unless (tempfile = file[:tempfile])

      a = Attachment.new(
        bucket: file[:container_id],
        filename: fobj['filename'],
        file_path: file[:tempfile],
        created_by: user_id,
        created_for: user_id,
        content_type: file[:type],
        attachable_type: map_info[key]['type'],
        attachable_id: element.id,
      )
      begin
        a.save!

        update_properties_upload(element, element.properties, a, pa)
        attach_ary.push(a.id)
      ensure
        tempfile.close
        tempfile.unlink
      end
    end
    element.send("#{type.downcase}s_revisions")&.last&.destroy!
    element.save!
  end
  attach_ary
end

#fetch_klass(name, id) ⇒ Object



14
15
16
17
18
# File 'lib/labimotion/helpers/generic_helpers.rb', line 14

def fetch_klass(name, id)
  klz = name.constantize.find(id)
  error!("#{name.gsub(/(Klass)/, '')} is invalid. Please re-select.", 500) if klz.nil?
  klz
end

#fetch_properties_uploads(properties) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/labimotion/helpers/generic_helpers.rb', line 28

def fetch_properties_uploads(properties)
  uploads = []
  properties['layers'].keys.each do |key|
    layer = properties['layers'][key]
    field_uploads = layer['fields'].select { |ss| ss['type'] == 'upload' }
    field_uploads.each do |field|
      ((field['value'] && field['value']['files']) || []).each do |file|
        uploads.push({ layer: key, field: field['field'], uid: file['uid'], filename: file['filename'] })
      end
    end
  end
  uploads
end

#fetch_repo_generic_template(klass, identifier) ⇒ Object



130
131
132
# File 'lib/labimotion/helpers/generic_helpers.rb', line 130

def fetch_repo_generic_template(klass, identifier)
  Chemotion::Generic::Fetch::Template.exec(API::TARGET, klass, identifier)
end

#fetch_repo_generic_template_list(name = false) ⇒ Object



134
135
136
# File 'lib/labimotion/helpers/generic_helpers.rb', line 134

def fetch_repo_generic_template_list(name = false)
  Chemotion::Generic::Fetch::Template.list(API::TARGET, name)
end

#generate_klass_fileObject



20
21
22
23
24
25
26
# File 'lib/labimotion/helpers/generic_helpers.rb', line 20

def generate_klass_file
  klass_dir = File.join(Rails.root, 'data')
  !File.directory?(klass_dir) && FileUtils.mkdir_p(klass_dir)
  klass_names_file = File.join(klass_dir, 'klasses.json')
  klasses = ElementKlass.where(is_active: true)&.pluck(:name) || []
  File.write(klass_names_file, klasses)
end

#update_properties_upload(element, properties, att, pa) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/labimotion/helpers/generic_helpers.rb', line 42

def update_properties_upload(element, properties, att, pa)
  return if pa.nil?

  idx = properties['layers'][pa[:layer]]['fields'].index { |fl| fl['field'] == pa[:field] }
  fidx = properties['layers'][pa[:layer]]['fields'][idx]['value']['files'].index { |fi| fi['uid'] == pa[:uid] }
  properties['layers'][pa[:layer]]['fields'][idx]['value']['files'][fidx]['aid'] = att.id
  properties['layers'][pa[:layer]]['fields'][idx]['value']['files'][fidx]['uid'] = att.identifier
  element.update_columns(properties: properties)
end