]> source.dussan.org Git - redmine.git/commitdiff
Support custom fields when exporting users to CSV (#33126).
authorGo MAEDA <maeda@farend.jp>
Sat, 4 Apr 2020 01:42:16 +0000 (01:42 +0000)
committerGo MAEDA <maeda@farend.jp>
Sat, 4 Apr 2020 01:42:16 +0000 (01:42 +0000)
Patch by Mizuki ISHIKAWA.

git-svn-id: http://svn.redmine.org/redmine/trunk@19642 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/users_helper.rb
test/functional/users_controller_test.rb

index a203e9a4c99f5320ce268f69b68ef76b391d8f22..2a72a5db47255f6efaea3051aa43bbd0b5304037 100644 (file)
@@ -82,16 +82,24 @@ module UsersHelper
         'last_login_on',
         'status'
       ]
+      user_custom_fields = UserCustomField.all
 
       # csv header fields
-      csv << columns.map{|column| l('field_' + column)}
+      csv << columns.map {|column| l('field_' + column)} + user_custom_fields.pluck(:name)
       # csv lines
+      users = users.preload(:custom_values)
       users.each do |user|
-        csv << columns.map do |column|
-          if column == 'status'
-            l(("status_#{User::LABEL_BY_STATUS[user.status]}"))
-          else
-            format_object(user.send(column), false)
+        values = columns.map {|c| c == 'status' ? l("status_#{User::LABEL_BY_STATUS[user.status]}") : user.send(c)} +
+                 user_custom_fields.map {|custom_field| user.custom_value_for(custom_field)}
+
+        csv << values.map do |value|
+          format_object(value, false) do |v|
+            case v.class.name
+            when 'Float'
+              sprintf('%.2f', v).gsub('.', l(:general_csv_decimal_separator))
+            else
+              v
+            end
           end
         end
       end
index 36ca9539f997668fc268130d2a43727897a8ca89..40105bb2e95166202c9f4ef76a3999aef35f0e37 100644 (file)
@@ -78,6 +78,24 @@ class UsersControllerTest < Redmine::ControllerTest
     end
   end
 
+  def test_index_csv_with_custom_field_columns
+    float_custom_field = UserCustomField.generate!(:name => 'float field', :field_format => 'float')
+    date_custom_field = UserCustomField.generate!(:name => 'date field', :field_format => 'date')
+    user = User.last
+    user.custom_field_values = {float_custom_field.id.to_s => 2.1, date_custom_field.id.to_s => '2020-01-10'}
+    user.save
+
+    User.find(@request.session[:user_id]).update(:language => nil)
+    with_settings :default_language => 'fr' do
+      get :index, :params => { :name => user.lastname, :format => 'csv' }
+      assert_response :success
+
+      assert_include 'float field;date field', response.body
+      assert_include '2,10;10/01/2020', response.body
+      assert_equal 'text/csv', @response.media_type
+    end
+  end
+
   def test_index_csv_with_status_filter
     with_settings :default_language => 'en' do
       get :index, :params => { :status => 3, :format => 'csv' }