'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
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' }