refactor(manager data exports): extract method to generate csv

This commit is contained in:
Eric Leroy-Terquem 2024-11-04 18:02:42 +01:00
parent d5de21fac4
commit 2ff224ec47
No known key found for this signature in database
GPG key ID: 53D8FAECEF207605
3 changed files with 17 additions and 24 deletions

View file

@ -42,24 +42,16 @@ module Manager
def export_last_half_year def export_last_half_year
administrateurs = Administrateur.joins(:user).where(created_at: 6.months.ago..).where.not(users: { email_verified_at: nil }) administrateurs = Administrateur.joins(:user).where(created_at: 6.months.ago..).where.not(users: { email_verified_at: nil })
csv = CSV.generate(headers: true) do |csv|
csv << ['ID', 'Email', 'Date de création'] csv = generate_csv(administrateurs)
administrateurs.each do |administrateur|
csv << [administrateur.id, administrateur.email, administrateur.created_at]
end
end
send_data csv, filename: "administrateurs_recents_#{Date.today.strftime('%d-%m-%Y')}.csv" send_data csv, filename: "administrateurs_recents_#{Date.today.strftime('%d-%m-%Y')}.csv"
end end
def export_with_publiee_procedure def export_with_publiee_procedure
administrateurs = Administrateur.joins(:user).where.not(users: { email_verified_at: nil }).joins(:procedures).where(procedures: { aasm_state: [:publiee] }) administrateurs = Administrateur.joins(:user).where.not(users: { email_verified_at: nil }).joins(:procedures).where(procedures: { aasm_state: [:publiee] })
csv = CSV.generate(headers: true) do |csv|
csv << ['ID', 'Email', 'Date de création'] csv = generate_csv(administrateurs)
administrateurs.each do |administrateur|
csv << [administrateur.id, administrateur.email, administrateur.created_at]
end
end
send_data csv, filename: "administrateurs_actifs_#{Date.today.strftime('%d-%m-%Y')}.csv" send_data csv, filename: "administrateurs_actifs_#{Date.today.strftime('%d-%m-%Y')}.csv"
end end

View file

@ -56,5 +56,14 @@ module Manager
payload[:to_log] = to_log payload[:to_log] = to_log
end end
def generate_csv(users)
CSV.generate(headers: true) do |csv|
csv << ['ID', 'Email', 'Date de création']
users.each do |user|
csv << [user.id, user.email, user.created_at]
end
end
end
end end
end end

View file

@ -25,24 +25,16 @@ module Manager
def export_last_half_year def export_last_half_year
instructeurs = Instructeur.joins(:user).where(created_at: 6.months.ago..).where.not(users: { email_verified_at: nil }) instructeurs = Instructeur.joins(:user).where(created_at: 6.months.ago..).where.not(users: { email_verified_at: nil })
csv = CSV.generate(headers: true) do |csv|
csv << ['ID', 'Email', 'Date de création'] csv = generate_csv(instructeurs)
instructeurs.each do |instructeur|
csv << [instructeur.id, instructeur.email, instructeur.created_at]
end
end
send_data csv, filename: "instructeurs_recents_#{Date.today.strftime('%d-%m-%Y')}.csv" send_data csv, filename: "instructeurs_recents_#{Date.today.strftime('%d-%m-%Y')}.csv"
end end
def export_currently_active def export_currently_active
instructeurs = Instructeur.joins(:user).where(users: { current_sign_in_at: 6.months.ago.. }).where.not(users: { email_verified_at: nil }) instructeurs = Instructeur.joins(:user).where(users: { current_sign_in_at: 6.months.ago.. }).where.not(users: { email_verified_at: nil })
csv = CSV.generate(headers: true) do |csv|
csv << ['ID', 'Email', 'Date de création'] csv = generate_csv(instructeurs)
instructeurs.each do |instructeur|
csv << [instructeur.id, instructeur.email, instructeur.created_at]
end
end
send_data csv, filename: "instructeurs_actifs_#{Date.today.strftime('%d-%m-%Y')}.csv" send_data csv, filename: "instructeurs_actifs_#{Date.today.strftime('%d-%m-%Y')}.csv"
end end