end
def issues_to_csv(issues, project, query, options={})
- decimal_separator = l(:general_csv_decimal_separator)
encoding = l(:general_csv_encoding)
columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
if options[:description]
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
# csv header fields
csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
-
# csv lines
issues.each do |issue|
- col_values = columns.collect do |column|
- s = if column.is_a?(QueryCustomFieldColumn)
- cv = issue.custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id}
- show_value(cv)
- else
- value = column.value(issue)
- if value.is_a?(Date)
- format_date(value)
- elsif value.is_a?(Time)
- format_time(value)
- elsif value.is_a?(Float)
- ("%.2f" % value).gsub('.', decimal_separator)
- else
- value
- end
- end
- s.to_s
- end
- csv << [ issue.id.to_s ] + col_values.collect {|c| Redmine::CodesetUtil.from_utf8(c.to_s, encoding) }
+ csv << [ issue.id.to_s ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, issue), encoding) }
end
end
export
end
end
+ def csv_content(column, issue)
+ value = column.value(issue)
+ if value.is_a?(Array)
+ value.collect {|v| csv_value(column, issue, v)}.compact.join(', ')
+ else
+ csv_value(column, issue, value)
+ end
+ end
+
+ def csv_value(column, issue, value)
+ case value.class.name
+ when 'Time'
+ format_time(value)
+ when 'Date'
+ format_date(value)
+ when 'Float'
+ sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator))
+ when 'IssueRelation'
+ other = value.other_issue(issue)
+ l(value.label_for(issue)) + " ##{other.id}"
+ else
+ value.to_s
+ end
+ end
+
# Retrieve query from session or build a new query
def retrieve_query
if !params[:query_id].blank?
assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
end
+ def test_index_csv_should_format_float_custom_fields_with_csv_decimal_separator
+ field = IssueCustomField.create!(:name => 'Float', :is_for_all => true, :tracker_ids => [1], :field_format => 'float')
+ issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id => '185.6'})
+
+ with_settings :default_language => 'fr' do
+ get :index, :format => 'csv', :columns => 'all'
+ assert_response :success
+ issue_line = response.body.chomp.split("\n").map {|line| line.split(';')}.detect {|line| line[0]==issue.id.to_s}
+ assert_include '185,60', issue_line
+ end
+
+ with_settings :default_language => 'en' do
+ get :index, :format => 'csv', :columns => 'all'
+ assert_response :success
+ issue_line = response.body.chomp.split("\n").map {|line| line.split(',')}.detect {|line| line[0]==issue.id.to_s}
+ assert_include '185.60', issue_line
+ end
+ end
+
def test_index_csv_big_5
with_settings :default_language => "zh-TW" do
str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
if str_utf8.respond_to?(:force_encoding)
s1.force_encoding('Big5')
end
- assert lines[0].include?(s1)
- assert lines[1].include?(str_big5)
+ assert_include s1, lines[0]
+ assert_include str_big5, lines[1]
end
end