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? layout: 'angular' # !request.xhr?
end end
format.csv do 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}" 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", send_data(serialized_work_packages, type: "text/csv; #{charset}; header=present",
filename: 'export.csv') filename: "#{title}.csv")
end end
format.pdf do format.pdf do
serialized_work_packages = WorkPackage::Exporter.pdf(@work_packages, serialized_work_packages = WorkPackage::Exporter.pdf(@work_packages,

@ -30,60 +30,17 @@
module WorkPackage::CsvExporter module WorkPackage::CsvExporter
include Redmine::I18n include Redmine::I18n
include CustomFieldsHelper include CustomFieldsHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::NumberHelper
def csv(work_packages, project = nil) def csv(work_packages, query)
decimal_separator = l(:general_csv_decimal_separator)
export = CSV.generate(col_sep: l(:general_csv_separator)) do |csv| export = CSV.generate(col_sep: l(:general_csv_separator)) do |csv|
# csv header fields headers = csv_headers(query)
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)
csv << encode_csv_columns(headers) csv << encode_csv_columns(headers)
# csv lines
work_packages.each do |work_package| work_packages.each do |work_package|
fields = [work_package.id, row = csv_row(work_package, query)
work_package.status.name, csv << encode_csv_columns(row)
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)
end end
end end
@ -95,4 +52,52 @@ module WorkPackage::CsvExporter
Redmine::CodesetUtil.from_utf8(cell.to_s, encoding) Redmine::CodesetUtil.from_utf8(cell.to_s, encoding)
end end
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 end

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

Loading…
Cancel
Save