Feature #33126 » feature-33126.patch
app/helpers/users_helper.rb | ||
---|---|---|
82 | 82 |
'last_login_on', |
83 | 83 |
'status' |
84 | 84 |
] |
85 |
user_custom_fields = UserCustomField.all |
|
85 | 86 | |
86 | 87 |
# csv header fields |
87 |
csv << columns.map{|column| l('field_' + column)} |
|
88 |
csv << columns.map{|column| l('field_' + column)} + user_custom_fields.pluck(:name)
|
|
88 | 89 |
# csv lines |
90 |
users = users.preload(:custom_values) |
|
89 | 91 |
users.each do |user| |
90 |
csv << columns.map do |column| |
|
91 |
if column == 'status' |
|
92 |
l(("status_#{User::LABEL_BY_STATUS[user.status]}")) |
|
93 |
else |
|
94 |
format_object(user.send(column), false) |
|
92 |
values = columns.map{|c| c == 'status' ? l("status_#{User::LABEL_BY_STATUS[user.status]}") : user.send(c)} + |
|
93 |
user_custom_fields.map{|custom_field| user.custom_value_for(custom_field) } |
|
94 | ||
95 |
csv << values.map do |value| |
|
96 |
format_object(value, false) do |v| |
|
97 |
case v.class.name |
|
98 |
when 'Float' |
|
99 |
sprintf("%.2f", v).gsub('.', l(:general_csv_decimal_separator)) |
|
100 |
else |
|
101 |
v |
|
102 |
end |
|
95 | 103 |
end |
96 | 104 |
end |
97 | 105 |
end |
test/functional/users_controller_test.rb | ||
---|---|---|
78 | 78 |
end |
79 | 79 |
end |
80 | 80 | |
81 |
def test_index_csv_with_custom_field_columns |
|
82 |
float_custom_field = UserCustomField.generate!(:name => 'float field', :field_format => 'float') |
|
83 |
date_custom_field = UserCustomField.generate!(:name => 'date field', :field_format => 'date') |
|
84 |
user = User.last |
|
85 |
user.custom_field_values = {float_custom_field.id.to_s => 2.1, date_custom_field.id.to_s => '2020-01-10'} |
|
86 |
user.save |
|
87 | ||
88 |
User.find(@request.session[:user_id]).update(:language => nil) |
|
89 |
with_settings :default_language => 'fr' do |
|
90 |
get :index, :params => { :name => user.lastname, :format => 'csv' } |
|
91 |
assert_response :success |
|
92 | ||
93 |
assert_include 'float field;date field', response.body |
|
94 |
assert_include '2,10;10/01/2020', response.body |
|
95 |
assert_equal 'text/csv', @response.media_type |
|
96 |
end |
|
97 |
end |
|
98 | ||
81 | 99 |
def test_index_csv_with_status_filter |
82 | 100 |
with_settings :default_language => 'en' do |
83 | 101 |
get :index, :params => { :status => 3, :format => 'csv' } |