mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-21 07:26:15 +08:00
Refactored a bit repository records CSV export. Fixed the order of CSV rows to match the currently viewed datatable order. [SCI-1275]
This commit is contained in:
parent
773a55ffac
commit
f46d8f6970
|
@ -223,57 +223,59 @@ class RepositoriesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_zip
|
def generate_zip
|
||||||
|
# Fetch rows in the same order as in the currently viewed datatable
|
||||||
|
ordered_row_ids = params[:row_ids]
|
||||||
|
id_row_map = RepositoryRow.where(id: ordered_row_ids).index_by(&:id)
|
||||||
|
ordered_rows = ordered_row_ids.collect { |id| id_row_map[id.to_i] }
|
||||||
|
|
||||||
zip = ZipExport.create(user: current_user)
|
zip = ZipExport.create(user: current_user)
|
||||||
zip.generate_exportable_zip(
|
zip.generate_exportable_zip(
|
||||||
current_user,
|
current_user,
|
||||||
to_csv(RepositoryRow.where(id: params[:row_ids]), params[:header_ids]),
|
to_csv(ordered_rows, params[:header_ids]),
|
||||||
:repositories
|
:repositories
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_csv(rows, header_ids)
|
def to_csv(rows, column_ids)
|
||||||
require 'csv'
|
require 'csv'
|
||||||
|
|
||||||
# Parse header IDs (magic numbers should be refactored - see
|
# Parse column names
|
||||||
# sample-datatable.js)
|
csv_header = []
|
||||||
header_names = []
|
column_ids.each do |c_id|
|
||||||
header_ids.each do |header|
|
csv_header << case c_id.to_i
|
||||||
if header == '-1'
|
when -1
|
||||||
next
|
next
|
||||||
elsif header == '-2'
|
when -2
|
||||||
header_names << I18n.t('repositories.table.row_name')
|
I18n.t('repositories.table.row_name')
|
||||||
elsif header == '-3'
|
when -3
|
||||||
header_names << I18n.t('repositories.table.added_by')
|
I18n.t('repositories.table.added_by')
|
||||||
elsif header == '-4'
|
when -4
|
||||||
header_names << I18n.t('repositories.table.added_on')
|
I18n.t('repositories.table.added_on')
|
||||||
else
|
else
|
||||||
rc = RepositoryColumn.find_by_id(header)
|
column = RepositoryColumn.find_by_id(c_id)
|
||||||
if rc
|
column ? column.name : nil
|
||||||
header_names << rc.name
|
end
|
||||||
else
|
|
||||||
header_names << nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
CSV.generate do |csv|
|
CSV.generate do |csv|
|
||||||
csv << header_names
|
csv << csv_header
|
||||||
rows.each do |row|
|
rows.each do |row|
|
||||||
csv_row = []
|
csv_row = []
|
||||||
header_ids.each do |header_id|
|
column_ids.each do |c_id|
|
||||||
if header_id == '-1'
|
csv_row << case c_id.to_i
|
||||||
next
|
when -1
|
||||||
elsif header_id == '-2'
|
next
|
||||||
csv_row << row.name
|
when -2
|
||||||
elsif header_id == '-3'
|
row.name
|
||||||
csv_row << row.created_by.full_name
|
when -3
|
||||||
elsif header_id == '-4'
|
row.created_by.full_name
|
||||||
csv_row << I18n.l(row.created_at, format: :full)
|
when -4
|
||||||
else
|
I18n.l(row.created_at, format: :full)
|
||||||
column = row.repository_cells
|
else
|
||||||
.find_by(repository_column_id: header_id)
|
cell = row.repository_cells
|
||||||
csv_row << (column ? column.value.data : nil)
|
.find_by(repository_column_id: c_id)
|
||||||
end
|
cell ? cell.value.data : nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
csv << csv_row
|
csv << csv_row
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue