]> source.dussan.org Git - redmine.git/commitdiff
Fixed that float custom fields do not use CSV decimal separator (#10364).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 19 Feb 2013 20:01:00 +0000 (20:01 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 19 Feb 2013 20:01:00 +0000 (20:01 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11441 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/issues_helper.rb
app/helpers/queries_helper.rb
test/functional/issues_controller_test.rb

index 7635c47ff7bdf8a4018b9897e40a588c9d5c72a5..73ea79cb737d2af159831ad56b43a087c6122689 100644 (file)
@@ -372,7 +372,6 @@ module IssuesHelper
   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]
@@ -384,28 +383,9 @@ module IssuesHelper
     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
index 6c8ba1a7f94812d22e18954a17a96f14e75c70d2..d140ab3a393930e3b2afe00a647b60f081bf11c5 100644 (file)
@@ -96,6 +96,31 @@ module QueriesHelper
     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?
index 8a147ad49a6ff929aa572284cd634e2122498a9f..65e28b5703fba7a3b1441b67ff230b07802e9b62 100644 (file)
@@ -433,6 +433,25 @@ class IssuesControllerTest < ActionController::TestCase
     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"
@@ -453,8 +472,8 @@ class IssuesControllerTest < ActionController::TestCase
       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