download users csv

pull/629/head
Pete Matsyburka 4 weeks ago
parent 89e797b95f
commit 2303c21cea

@ -16,7 +16,19 @@ class UsersController < ApplicationController
@users.active.where.not(role: 'integration') @users.active.where.not(role: 'integration')
end end
@pagy, @users = pagy(@users.preload(account: :account_accesses).where(account: current_account).order(id: :desc)) @users = @users.preload(account: :account_accesses).where(account: current_account).order(id: :desc)
respond_to do |format|
format.html do
@pagy, @users = pagy(@users)
end
if current_ability.can?(:manage, current_account)
format.csv do
send_data Users.generate_csv(@users), filename: "users-#{Time.current.iso8601}.csv", type: 'text/csv'
end
end
end
end end
def new; end def new; end

@ -1,15 +1,27 @@
<div class="flex items-center space-x-1"> <div class="flex items-center space-x-1">
<% if (is_with_download = can?(:manage, :download_users) && can?(:manage, current_account)) %>
<%= button_to(t(:download), url_for(format: :csv), class: 'link text-sm', form: { class: 'flex' }, method: :get) %>
<% end %>
<% if %w[archived integration].include?(params[:status]) %> <% if %w[archived integration].include?(params[:status]) %>
<% if is_with_download %>
<span class="text-neutral-700">|</span>
<% end %>
<%= link_to t('view_active'), settings_users_path, class: 'link text-sm' %> <%= link_to t('view_active'), settings_users_path, class: 'link text-sm' %>
<% else %> <% else %>
<% archived_exists = current_account.users.archived.where.not(role: 'integration').exists? %> <% archived_exists = current_account.users.archived.where.not(role: 'integration').exists? %>
<% if current_account.users.active.exists?(role: 'integration') %> <% if (integration_exists = current_account.users.active.exists?(role: 'integration')) %>
<% if is_with_download %>
<span class="text-neutral-700">|</span>
<% end %>
<%= link_to t('view_embedding_users'), settings_integration_users_path, class: 'link text-sm' %> <%= link_to t('view_embedding_users'), settings_integration_users_path, class: 'link text-sm' %>
<% if archived_exists %> <% if archived_exists %>
<span class="text-neutral-700">|</span> <span class="text-neutral-700">|</span>
<% end %> <% end %>
<% end %> <% end %>
<% if archived_exists %> <% if archived_exists %>
<% if !integration_exists && is_with_download %>
<span class="text-neutral-700">|</span>
<% end %>
<%= link_to t('view_archived'), settings_archived_users_path, class: 'link text-sm' %> <%= link_to t('view_archived'), settings_archived_users_path, class: 'link text-sm' %>
<% end %> <% end %>
<% end %> <% end %>

@ -0,0 +1,15 @@
# frozen_string_literal: true
module Users
module_function
def generate_csv(users)
headers = %w[email first_name last_name role current_sign_in_at last_sign_in_at updated_at created_at]
CSVSafe.generate do |csv|
csv << headers
users.each { |user| csv << user.values_at(*headers) }
end
end
end
Loading…
Cancel
Save