Add CSV download for User

This commit is contained in:
Patrick Gansterer 2021-03-07 18:13:10 +01:00
parent 47d9c79617
commit d48320f9e0
3 changed files with 46 additions and 0 deletions

View file

@ -5,6 +5,10 @@ class Admin::UsersController < Admin::BaseController
@users = params[:show_deleted] ? User.deleted : User.undeleted
@users = @users.includes(:mail_delivery_status)
if request.format.csv?
send_data UsersCsv.new(@users).to_csv, filename: 'users.csv', type: 'text/csv'
end
# if somebody uses the search field:
@users = @users.natural_search(params[:user_name]) unless params[:user_name].blank?

View file

@ -2,6 +2,9 @@
- content_for :actionbar do
= link_to t('.new_user'), new_admin_user_path, class: 'btn btn-primary'
= link_to url_for(search: params[:q], format: :csv), class: 'btn' do
= glyph :download
CSV
- content_for :sidebar do
%p= t('.first_paragraph', url: link_to(t('.new_users'), new_admin_user_path)).html_safe

39
lib/users_csv.rb Normal file
View file

@ -0,0 +1,39 @@
class UsersCsv < RenderCSV
include ApplicationHelper
def header
row = [
User.human_attribute_name(:id),
User.human_attribute_name(:name),
User.human_attribute_name(:nick),
User.human_attribute_name(:first_name),
User.human_attribute_name(:last_name),
User.human_attribute_name(:email),
User.human_attribute_name(:phone),
User.human_attribute_name(:last_login),
User.human_attribute_name(:last_activity),
User.human_attribute_name(:iban),
User.human_attribute_name(:ordergroup)
]
row + User.custom_fields.pluck(:label)
end
def data
@object.each do |o|
row = [
o.id,
o.name,
o.nick,
o.first_name,
o.last_name,
o.email,
o.phone,
o.last_login,
o.last_activity,
o.iban,
o.ordergroup&.name
]
yield row + User.custom_fields.map { |f| o.settings.custom_fields[f[:name]] }
end
end
end