scinote-web/app/services/import_repository/parse_repository.rb

49 lines
1.3 KiB
Ruby
Raw Normal View History

2017-06-19 20:05:37 +08:00
module ImportRepository
class ParseRepository
include ActionView::Helpers::TextHelper
def initialize(options)
@file = options.fetch(:file)
@repository = options.fetch(:repository)
@session = options.fetch(:session)
@sheet = SpreadsheetParser.open_spreadsheet(@file)
2017-06-19 20:05:37 +08:00
end
def data
header, columns = SpreadsheetParser.first_two_rows(@sheet)
2017-06-19 20:05:37 +08:00
# Fill in fields for dropdown
@repository.importable_repository_fields.transform_values! do |name|
2017-06-19 20:05:37 +08:00
truncate(name, length: Constants::NAME_TRUNCATION_LENGTH_DROPDOWN)
end
Data.new(header,
columns,
@repository.importable_repository_fields,
@repository)
2017-06-19 20:05:37 +08:00
end
def too_large?
@file.size > Rails.configuration.x.file_max_size_mb.megabytes
2017-06-19 20:05:37 +08:00
end
def has_too_many_rows?
@sheet.last_row > Constants::IMPORT_REPOSITORY_ITEMS_LIMIT
end
def generate_temp_file
2017-06-19 20:05:37 +08:00
# Save file for next step (importing)
temp_file = TempFile.new(
2017-06-19 20:05:37 +08:00
session_id: @session.id,
file: @file
)
if temp_file.save
TempFile.destroy_obsolete(temp_file.id)
return temp_file
2017-06-19 20:05:37 +08:00
end
end
Data = Struct.new(
:header, :columns, :available_fields, :repository
2017-06-19 20:05:37 +08:00
)
end
end