Class: ColActiveImporterStarter::BaseImporter

Inherits:
ActiveImporter::Base
  • Object
show all
Defined in:
lib/col_active_importer_starter/lib.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#end_atObject (readonly)

Returns the value of attribute end_at.



44
45
46
# File 'lib/col_active_importer_starter/lib.rb', line 44

def end_at
  @end_at
end

#start_atObject (readonly)

Returns the value of attribute start_at.



44
45
46
# File 'lib/col_active_importer_starter/lib.rb', line 44

def start_at
  @start_at
end

#time_consumingObject (readonly)

Returns the value of attribute time_consuming.



44
45
46
# File 'lib/col_active_importer_starter/lib.rb', line 44

def time_consuming
  @time_consuming
end

Instance Method Details

#get_result_id_column_indexObject



46
47
48
49
50
# File 'lib/col_active_importer_starter/lib.rb', line 46

def get_result_id_column_index
  return -1 if params.blank?

  params[ResultIndex::ID]
end

#handle_import_finishedObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/col_active_importer_starter/lib.rb', line 144

def handle_import_finished
  exporter = @exporter

  return if params.blank?

  file = params[:file]

  extname = '.xlsx'
  if file.present?
    extname = File.extname(file)
    basename = File.basename(file, extname)
  end

  result_file = "tmp/Result-#{Time.now.strftime('%Y%m%d%H%M%S')}-#{basename}#{extname}"

  exporter.write result_file
end

#handle_row_error(e) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/col_active_importer_starter/lib.rb', line 112

def handle_row_error(e)
  exporter_sheet1 = @exporter_sheet1

  row_index = @row_index - @header_index
  column_index = get_result_id_column_index

  return if invalid_column_index?(column_index)

  column_index += 1
  exporter_sheet1.add_cell(row_index, (column_index += 1), ResultValue::Fail)
  exporter_sheet1.add_cell(row_index, (column_index += 1), e.message)
end

#handle_row_processedObject



129
130
131
132
133
134
135
136
137
138
# File 'lib/col_active_importer_starter/lib.rb', line 129

def handle_row_processed
  exporter_sheet1 = @exporter_sheet1

  row_index = @row_index - @header_index
  column_index = get_result_id_column_index

  return if invalid_column_index?(column_index)

  exporter_sheet1.add_cell(row_index, (column_index += 1), model&.id)
end

#handle_row_successObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/col_active_importer_starter/lib.rb', line 96

def handle_row_success
  exporter_sheet1 = @exporter_sheet1

  row_index = @row_index - @header_index
  column_index = get_result_id_column_index

  return if invalid_column_index?(column_index)

  column_index += 1
  exporter_sheet1.add_cell(row_index, (column_index += 1), ResultValue::Success)
end

#infoObject



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/col_active_importer_starter/lib.rb', line 168

def info
  {
    start_at: start_at,
    end_at: end_at,
    time_consuming: time_consuming,

    row_count: row_count,
    row_processed_count: row_processed_count,
    row_success_count: row_success_count,
    row_error_count: row_error_count,
  }
end

#initialize_model_cacheObject



70
71
72
73
74
75
76
# File 'lib/col_active_importer_starter/lib.rb', line 70

def initialize_model_cache
  return if params.blank?

  model_cache = params.try(:[], :model_cache) || {}

  @model_cache = model_cache
end

#initialize_result_exporterObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/col_active_importer_starter/lib.rb', line 52

def initialize_result_exporter
  return if params.blank?

  exporter = RubyXL::Parser.parse(params[:file])
  exporter_sheet1 = exporter[0]

  header_index = @header_index - 1
  column_index = get_result_id_column_index

  return if invalid_column_index?(column_index)

  exporter_sheet1.add_cell(header_index, (column_index += 1), ResultColumnName::ID)
  exporter_sheet1.add_cell(header_index, (column_index += 1), ResultColumnName::Message)

  @exporter = exporter
  @exporter_sheet1 = exporter_sheet1
end