]> source.dussan.org Git - redmine.git/commitdiff
Refactor: #issues_to_csv and #entries_to_csv merged into QueriesHelper#query_to_csv.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 23 Feb 2013 11:07:43 +0000 (11:07 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 23 Feb 2013 11:07:43 +0000 (11:07 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11448 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/issues_controller.rb
app/controllers/timelog_controller.rb
app/helpers/issues_helper.rb
app/helpers/queries_helper.rb
app/helpers/timelog_helper.rb
test/functional/issues_controller_test.rb

index 11469a20b78e4134b8d2ab3a4603704ad95ff8a2..314460c6fc080258e7c2395e45d33bcbec9d8548 100644 (file)
@@ -85,7 +85,7 @@ class IssuesController < ApplicationController
           Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
         }
         format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
-        format.csv  { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
+        format.csv  { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
         format.pdf  { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
       end
     else
index e90442c681500acbc6e3a4f968fd310d01b3cc6d..92210fff59d31712772bd53f2e53e7221ab1ce35 100644 (file)
@@ -87,7 +87,7 @@ class TimelogController < ApplicationController
           :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
           :order => sort_clause
         )
-        send_data(entries_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
+        send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
       }
     end
   end
index a0c48e20d905e11b213c3d8a0ea14e3b4c2a4e95..59cff5ec11c6248d331fb5a17f029e43d453ac5a 100644 (file)
@@ -370,24 +370,4 @@ module IssuesHelper
       end
     end
   end
-
-  def issues_to_csv(issues, project, query, options={})
-    encoding = l(:general_csv_encoding)
-    columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
-    if options[:description]
-      if description = query.available_columns.detect {|q| q.name == :description}
-        columns << description
-      end
-    end
-
-    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|
-        csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, issue), encoding) }
-      end
-    end
-    export
-  end
 end
index 80bceaf1000248ca1c5accde79650b1d52207bfd..2001a59e67c1e0f79cdf03363881f848f531f297 100644 (file)
@@ -123,6 +123,26 @@ module QueriesHelper
     end
   end
 
+  def query_to_csv(items, query, options={})
+    encoding = l(:general_csv_encoding)
+    columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
+    query.available_block_columns.each do |column|
+      if options[column.name].present?
+        columns << column
+      end
+    end
+
+    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
+      items.each do |item|
+        csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, item), encoding) }
+      end
+    end
+    export
+  end
+
   # Retrieve query from session or build a new query
   def retrieve_query
     if !params[:query_id].blank?
index 380c4721c73ebba051e35c01948baff002f0cfbf..f6bdba2ec82c70bd73e7a46c711795a862ed240e 100644 (file)
@@ -86,21 +86,6 @@ module TimelogHelper
                         value)
   end
 
-  def entries_to_csv(entries, query, options={})
-    encoding = l(:general_csv_encoding)
-    columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
-
-    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
-      entries.each do |entry|
-        csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, entry), encoding) }
-      end
-    end
-    export
-  end
-
   def format_criteria_value(criteria_options, value)
     if value.blank?
       "[#{l(:label_none)}]"
index df3c2e330b52b237ff6bcf5aaca02731c7ae5535..a9cf4614076eedb3caba90c341d0301fb156e7cd 100644 (file)
@@ -391,13 +391,18 @@ class IssuesControllerTest < ActionController::TestCase
   end
 
   def test_index_csv_with_description
-    get :index, :format => 'csv', :description => '1'
-    assert_response :success
-    assert_not_nil assigns(:issues)
-    assert_equal 'text/csv; header=present', @response.content_type
-    assert @response.body.starts_with?("#,")
-    lines = @response.body.chomp.split("\n")
-    assert_equal assigns(:query).columns.size + 1, lines[0].split(',').size
+    Issue.generate!(:description => 'test_index_csv_with_description')
+
+    with_settings :default_language => 'en' do
+      get :index, :format => 'csv', :description => '1'
+      assert_response :success
+      assert_not_nil assigns(:issues)
+    end
+
+    assert_equal 'text/csv; header=present', response.content_type
+    headers = response.body.chomp.split("\n").first.split(',')
+    assert_include 'Description', headers
+    assert_include 'test_index_csv_with_description', response.body
   end
 
   def test_index_csv_with_spent_time_column