Class: CrujCrujCruj::Services::ImportRules

Inherits:
Object
  • Object
show all
Defined in:
lib/cruj_cruj_cruj/services/import_rules.rb

Class Method Summary collapse

Class Method Details

.export_template(fields, data) ⇒ Object



6
7
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
# File 'lib/cruj_cruj_cruj/services/import_rules.rb', line 6

def self.export_template(fields, data)
  package          = Axlsx::Package.new
  workbook         = package.workbook

  parameters_sheet_name = 'PARAMETERS'

  workbook.add_worksheet(name: 'TEMPLATE') do |sheet|
    title = sheet.styles.add_style(bg_color: 'FF007E7A', fg_color: 'FFFFFFFF', sz: 12,  b: true, border: {style: :thin, color: 'FF000000'})

    fields.each_with_index do |field, idx|
      validation  = field[:data_validation][:validation]
      allow_blank = field[:data_validation][:allow_blank]
      add_data_validation(sheet, idx + 1, validation, allow_blank, parameters_sheet_name) if validation
    end

    sheet.add_row (([:id] |fields.map { |field| field[:field_name] }).map { |t| I18n.t("#{t}_label") }), style: title

    data.map { |resource| [resource.id].concat(fields.map { |field| resource_field_value(resource, field) }) }.each do |row|
      sheet.add_row row, types: row.map{ |_| :string }
    end
  end

  create_validations(fields, workbook, parameters_sheet_name)

  filename = "tmp/template_#{Time.zone.now.strftime('%Y%m%d%H%M%S')}.xlsx"
  package.serialize(filename)
  filename
end

.import(file, fields, clazz) ⇒ Object



35
36
37
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
75
# File 'lib/cruj_cruj_cruj/services/import_rules.rb', line 35

def self.import(file, fields, clazz)
  spreadsheet = Roo::Spreadsheet.open(file.path, extension: :xlsx)

  errors = []
  
  if Rails::VERSION::MAJOR < 4
    invalid_rows = validate_id_array(spreadsheet, clazz)
  else
    invalid_rows = validate_id(spreadsheet, clazz)
  end
  errors << I18n.t(:column_invalid_rows, column: :id, rows: invalid_rows.first(25).join(', ')) unless invalid_rows.blank?

  fields.each_with_index do |field, idx|
    col = idx + 1
    unless field[:data_validation][:allow_blank]
      invalid_rows = validate_not_blank(col, spreadsheet)
      errors << I18n.t(:column_invalid_rows, column: field[:field_name], rows: invalid_rows.first(25).join(', ')) unless invalid_rows.blank?
    end

    if field[:data_validation][:validation]
      invalid_rows = validate_in_array(col, field_validation_values(field), spreadsheet)
      errors << I18n.t(:column_invalid_rows, column: field[:field_name], rows: invalid_rows.first(25).join(', ')) unless invalid_rows.blank?
    end
  end

  return errors unless errors.blank?


  (2..spreadsheet.last_row).each do |i|
    row = spreadsheet.row(i)
    id  = row[0]
    resource = id ? clazz.find(id) : clazz.new

    fields.each_with_index do |field, idx|
      resource.send("#{field[:field_name]}=", row_value(row, idx+1, field, spreadsheet))
      resource.save!
    end
  end

  {}
end