Merge branch 'release/4.0' into dev

Conflicts:
	app/controllers/work_packages_controller.rb
	app/models/work_package/csv_exporter.rb
	spec/controllers/work_packages_controller_spec.rb
pull/2369/merge
Jens Ulferts 10 years ago
commit 6e11dc44c4
  1. 5
      app/controllers/work_packages_controller.rb
  2. 105
      app/models/work_package/csv_exporter.rb
  3. 7
      spec/controllers/work_packages_controller_spec.rb

@ -221,11 +221,12 @@ class WorkPackagesController < ApplicationController
layout: 'angular' # !request.xhr?
end
format.csv do
serialized_work_packages = WorkPackage::Exporter.csv(@work_packages, @project)
serialized_work_packages = WorkPackage::Exporter.csv(@work_packages, @query)
charset = "charset=#{l(:general_csv_encoding).downcase}"
title = @query.new_record? ? l(:label_work_package_plural) : @query.name
send_data(serialized_work_packages, type: "text/csv; #{charset}; header=present",
filename: 'export.csv')
filename: "#{title}.csv")
end
format.pdf do
serialized_work_packages = WorkPackage::Exporter.pdf(@work_packages,

@ -30,60 +30,17 @@
module WorkPackage::CsvExporter
include Redmine::I18n
include CustomFieldsHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::NumberHelper
def csv(work_packages, project = nil)
decimal_separator = l(:general_csv_decimal_separator)
def csv(work_packages, query)
export = CSV.generate(col_sep: l(:general_csv_separator)) do |csv|
# csv header fields
headers = ['#',
WorkPackage.human_attribute_name(:status),
WorkPackage.human_attribute_name(:project),
WorkPackage.human_attribute_name(:type),
WorkPackage.human_attribute_name(:priority),
WorkPackage.human_attribute_name(:subject),
WorkPackage.human_attribute_name(:assigned_to),
WorkPackage.human_attribute_name(:category),
WorkPackage.human_attribute_name(:fixed_version),
WorkPackage.human_attribute_name(:author),
WorkPackage.human_attribute_name(:start_date),
WorkPackage.human_attribute_name(:due_date),
WorkPackage.human_attribute_name(:done_ratio),
WorkPackage.human_attribute_name(:estimated_hours),
WorkPackage.human_attribute_name(:parent_work_package),
WorkPackage.human_attribute_name(:created_at),
WorkPackage.human_attribute_name(:updated_at)
]
# Export project custom fields if project is given
# otherwise export custom fields marked as "For all projects"
custom_fields = project.nil? ? WorkPackageCustomField.for_all : project.all_work_package_custom_fields
custom_fields.each { |f| headers << f.name }
# Description in the last column
headers << CustomField.human_attribute_name(:description)
headers = csv_headers(query)
csv << encode_csv_columns(headers)
# csv lines
work_packages.each do |work_package|
fields = [work_package.id,
work_package.status.name,
work_package.project.name,
work_package.type.name,
work_package.priority.name,
work_package.subject,
work_package.assigned_to,
work_package.category,
work_package.fixed_version,
work_package.author.name,
format_date(work_package.start_date),
format_date(work_package.due_date),
(Setting.work_package_done_ratio != 'disabled' ? work_package.done_ratio : ''),
work_package.estimated_hours.to_s.gsub('.', decimal_separator),
work_package.parent_id,
format_time(work_package.created_at),
format_time(work_package.updated_at)
]
custom_fields.each { |f| fields << show_value(work_package.custom_value_for(f)) }
fields << work_package.description
csv << encode_csv_columns(fields)
row = csv_row(work_package, query)
csv << encode_csv_columns(row)
end
end
@ -95,4 +52,52 @@ module WorkPackage::CsvExporter
Redmine::CodesetUtil.from_utf8(cell.to_s, encoding)
end
end
private
# fetch all headers
def csv_headers(query)
headers = []
headers << '#'
query.columns.each_with_index do |column, _|
headers << column.caption
end
headers << CustomField.human_attribute_name(:description)
headers
end
# fetch all row values
def csv_row(work_package, query)
row = query.columns.collect do |column|
csv_format_value(work_package, column)
end
if row.size > 0
row.unshift(work_package.id.to_s)
row << work_package.description.gsub(/\r/, '').gsub(/\n/, ' ')
end
row
end
def csv_format_value(work_package, column)
if column.is_a?(QueryCustomFieldColumn)
cv = work_package.custom_values.detect { |v| v.custom_field_id == column.custom_field.id }
show_value(cv)
else
value = work_package.send(column.name)
case value
when Date
format_date(value)
when Time
format_time(value)
else
value
end
end.to_s
end
end

@ -193,12 +193,13 @@ describe WorkPackagesController, type: :controller do
before do
mock_csv = double('csv export')
expect(WorkPackage::Exporter).to receive(:csv).with(work_packages, project)
.and_return(mock_csv)
expect(WorkPackage::Exporter).to receive(:csv).with(work_packages, query)
.and_return(mock_csv)
expect(controller).to receive(:send_data).with(mock_csv,
type: 'text/csv; charset=utf-8; header=present',
filename: 'export.csv') do |*_args|
filename: "#{query.name}.csv") do |_|
# We need to render something because otherwise
# the controller will and he will not find a suitable template
controller.render text: 'success'

Loading…
Cancel
Save