Class: ImpExp::Importers::Xlsx

Inherits:
Base
  • Object
show all
Defined in:
app/services/imp_exp/importers/xlsx.rb

Constant Summary

Constants inherited from Base

Base::ROW_OFFSET

Instance Attribute Summary collapse

Attributes inherited from Base

#errors, #exception_notifier, #imported, #logger, #models, #scoping_parent

Instance Method Summary collapse

Methods inherited from Base

#import, #import_model_data, #import_result

Constructor Details

#initialize(scoping_parent, models, logger: nil, extra_format_validator_klass: nil, exception_notifier: nil, extra_data_validator_klass: nil) ⇒ Xlsx

rubocop : disable Metrics/ParameterLists



10
11
12
13
14
15
16
17
18
# File 'app/services/imp_exp/importers/xlsx.rb', line 10

def initialize(scoping_parent, models, logger: nil, extra_format_validator_klass: nil, exception_notifier: nil,
               extra_data_validator_klass: nil)
  # rubocop : enable Metrics/ParameterLists
  super(scoping_parent, models, logger: logger, exception_notifier: exception_notifier)
  @extra_format_validator_klass = extra_format_validator_klass
  @extra_data_validator_klass = extra_data_validator_klass
  @format_errors = []
  @data_validation_errors = []
end

Instance Attribute Details

#data_validation_errorsObject (readonly)

Returns the value of attribute data_validation_errors.



6
7
8
# File 'app/services/imp_exp/importers/xlsx.rb', line 6

def data_validation_errors
  @data_validation_errors
end

#extra_data_validator_klassObject (readonly)

Returns the value of attribute extra_data_validator_klass.



6
7
8
# File 'app/services/imp_exp/importers/xlsx.rb', line 6

def extra_data_validator_klass
  @extra_data_validator_klass
end

#extra_format_validator_klassObject (readonly)

Returns the value of attribute extra_format_validator_klass.



6
7
8
# File 'app/services/imp_exp/importers/xlsx.rb', line 6

def extra_format_validator_klass
  @extra_format_validator_klass
end

#format_errorsObject (readonly)

Returns the value of attribute format_errors.



6
7
8
# File 'app/services/imp_exp/importers/xlsx.rb', line 6

def format_errors
  @format_errors
end

#xlsx_readerObject (readonly)

Returns the value of attribute xlsx_reader.



6
7
8
# File 'app/services/imp_exp/importers/xlsx.rb', line 6

def xlsx_reader
  @xlsx_reader
end

Instance Method Details

#call(xlsx_stream, files_directory_path = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/imp_exp/importers/xlsx.rb', line 20

def call(xlsx_stream, files_directory_path = nil)
  ping(forced: true, stage: "validation")
  logger.info "validating xlsx import file"

  @xlsx_reader = ImpExp::Utils::XlsxReader.new(xlsx_stream)

  logger.info "validating sheets..."
  validate_presence_of_sheets(xlsx_reader.sheet_names)

  logger.info "validating columns..."
  validate_presence_of_columns(xlsx_reader)

  format_errors.concat extra_format_validator_klass.new(self).call if extra_format_validator_klass

  logger.info "validating data..."
  validate_data(xlsx_reader)

  import_result = if format_errors.none? && data_validation_errors.none?
                    data = models.index_with { |model| xlsx_reader.sheet_data(model.model_name) }
                    super(data, files_directory_path)
                  else
                    { imported: {}, import_errors: [] }
                  end

  { format_errors: format_errors, data_validation_errors: data_validation_errors }.merge(import_result)
end