2018-11-07 12:04:29 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-05 22:54:04 +08:00
|
|
|
require 'csv'
|
2018-11-07 12:04:29 +08:00
|
|
|
|
2018-03-05 22:54:04 +08:00
|
|
|
module RepositoryZipExport
|
2022-06-10 21:46:22 +08:00
|
|
|
def self.to_csv(rows, column_ids, user, repository, handle_file_name_func = nil, in_module = false)
|
2018-03-05 22:54:04 +08:00
|
|
|
# Parse column names
|
|
|
|
csv_header = []
|
2022-06-10 21:46:22 +08:00
|
|
|
add_consumption = in_module && !repository.is_a?(RepositorySnapshot) && repository.has_stock_management?
|
2018-03-05 22:54:04 +08:00
|
|
|
column_ids.each do |c_id|
|
|
|
|
csv_header << case c_id.to_i
|
|
|
|
when -1, -2
|
|
|
|
next
|
|
|
|
when -3
|
2018-04-04 17:55:11 +08:00
|
|
|
I18n.t('repositories.table.id')
|
2018-03-05 22:54:04 +08:00
|
|
|
when -4
|
2018-04-04 17:55:11 +08:00
|
|
|
I18n.t('repositories.table.row_name')
|
2018-03-05 22:54:04 +08:00
|
|
|
when -5
|
2018-04-04 17:55:11 +08:00
|
|
|
I18n.t('repositories.table.added_by')
|
|
|
|
when -6
|
2018-03-05 22:54:04 +08:00
|
|
|
I18n.t('repositories.table.added_on')
|
2020-06-22 20:28:49 +08:00
|
|
|
when -7
|
|
|
|
I18n.t('repositories.table.archived_by')
|
|
|
|
when -8
|
|
|
|
I18n.t('repositories.table.archived_on')
|
2018-03-05 22:54:04 +08:00
|
|
|
else
|
2023-06-12 16:29:17 +08:00
|
|
|
column = repository.repository_columns.find_by(id: c_id)
|
2018-03-05 22:54:04 +08:00
|
|
|
column ? column.name : nil
|
|
|
|
end
|
|
|
|
end
|
2022-06-03 16:10:00 +08:00
|
|
|
csv_header << I18n.t('repositories.table.row_consumption') if add_consumption
|
2018-03-05 22:54:04 +08:00
|
|
|
|
|
|
|
CSV.generate do |csv|
|
|
|
|
csv << csv_header
|
|
|
|
rows.each do |row|
|
|
|
|
csv_row = []
|
|
|
|
column_ids.each do |c_id|
|
|
|
|
csv_row << case c_id.to_i
|
|
|
|
when -1, -2
|
|
|
|
next
|
|
|
|
when -3
|
2023-02-06 18:18:19 +08:00
|
|
|
repository.is_a?(RepositorySnapshot) ? row.parent_id : row.id
|
2018-03-05 22:54:04 +08:00
|
|
|
when -4
|
2018-04-04 17:55:11 +08:00
|
|
|
row.name
|
2018-03-05 22:54:04 +08:00
|
|
|
when -5
|
2018-04-04 17:55:11 +08:00
|
|
|
row.created_by.full_name
|
|
|
|
when -6
|
2018-03-05 22:54:04 +08:00
|
|
|
I18n.l(row.created_at, format: :full)
|
2020-06-22 20:28:49 +08:00
|
|
|
when -7
|
2023-07-07 17:08:28 +08:00
|
|
|
row.archived? && row.archived_by.present? ? row.archived_by.full_name : ''
|
2020-06-22 20:28:49 +08:00
|
|
|
when -8
|
2023-07-07 17:08:28 +08:00
|
|
|
row.archived? && row.archived_on.present? ? I18n.l(row.archived_on, format: :full) : ''
|
2018-03-05 22:54:04 +08:00
|
|
|
else
|
2023-07-07 17:08:28 +08:00
|
|
|
cell = row.repository_cells.find_by(repository_column_id: c_id)
|
2018-09-17 05:01:44 +08:00
|
|
|
|
2018-04-24 20:53:49 +08:00
|
|
|
if cell
|
2023-06-12 16:29:17 +08:00
|
|
|
if cell.value_type == 'RepositoryAssetValue' && handle_file_name_func
|
2018-09-17 05:01:44 +08:00
|
|
|
handle_file_name_func.call(cell.value.asset)
|
|
|
|
else
|
2018-09-23 17:14:59 +08:00
|
|
|
SmartAnnotations::TagToText.new(
|
2022-06-03 16:10:00 +08:00
|
|
|
user, repository.team, cell.value.export_formatted
|
2018-09-23 17:14:59 +08:00
|
|
|
).text
|
2018-09-17 05:01:44 +08:00
|
|
|
end
|
2018-04-24 20:53:49 +08:00
|
|
|
end
|
2018-03-05 22:54:04 +08:00
|
|
|
end
|
|
|
|
end
|
2022-06-03 16:10:00 +08:00
|
|
|
|
|
|
|
csv_row << row.row_consumption(row.stock_consumption) if add_consumption
|
2018-03-05 22:54:04 +08:00
|
|
|
csv << csv_row
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|