2017-06-19 14:05:37 +02:00
|
|
|
module ImportRepository
|
|
|
|
class ParseRepository
|
|
|
|
include ActionView::Helpers::TextHelper
|
|
|
|
def initialize(options)
|
|
|
|
@file = options.fetch(:file)
|
|
|
|
@repository = options.fetch(:repository)
|
|
|
|
@session = options.fetch(:session)
|
2017-10-17 14:42:06 +02:00
|
|
|
@sheet = SpreadsheetParser.open_spreadsheet(@file)
|
2017-06-19 14:05:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2017-10-17 14:42:06 +02:00
|
|
|
header, columns = SpreadsheetParser.first_two_rows(@sheet)
|
2017-06-19 14:05:37 +02:00
|
|
|
# Fill in fields for dropdown
|
2018-03-13 11:36:05 +01:00
|
|
|
@repository.importable_repository_fields.transform_values! do |name|
|
2017-06-19 14:05:37 +02:00
|
|
|
truncate(name, length: Constants::NAME_TRUNCATION_LENGTH_DROPDOWN)
|
|
|
|
end
|
|
|
|
Data.new(header,
|
2017-07-18 14:54:35 +02:00
|
|
|
columns,
|
2018-03-13 11:36:05 +01:00
|
|
|
@repository.importable_repository_fields,
|
2017-10-12 14:43:25 +02:00
|
|
|
@repository)
|
2017-06-19 14:05:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def too_large?
|
2018-06-19 14:15:14 +02:00
|
|
|
@file.size > Rails.configuration.x.file_max_size_mb.megabytes
|
2017-06-19 14:05:37 +02:00
|
|
|
end
|
|
|
|
|
2018-05-31 10:45:31 +02:00
|
|
|
def has_too_many_rows?
|
|
|
|
@sheet.last_row > Constants::IMPORT_REPOSITORY_ITEMS_LIMIT
|
|
|
|
end
|
|
|
|
|
2017-10-12 14:43:25 +02:00
|
|
|
def generate_temp_file
|
2017-06-19 14:05:37 +02:00
|
|
|
# Save file for next step (importing)
|
2017-10-12 14:43:25 +02:00
|
|
|
temp_file = TempFile.new(
|
2017-06-19 14:05:37 +02:00
|
|
|
session_id: @session.id,
|
|
|
|
file: @file
|
|
|
|
)
|
|
|
|
|
2017-10-12 14:43:25 +02:00
|
|
|
if temp_file.save
|
2018-09-21 17:46:18 +02:00
|
|
|
TempFile.destroy_obsolete(temp_file.id)
|
2017-10-12 14:43:25 +02:00
|
|
|
return temp_file
|
2017-06-19 14:05:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Data = Struct.new(
|
2017-10-12 14:43:25 +02:00
|
|
|
:header, :columns, :available_fields, :repository
|
2017-06-19 14:05:37 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|