From 318b7878d82e20d29282a885f54a83f941d20bd7 Mon Sep 17 00:00:00 2001 From: Hugo Peixoto Date: Sun, 19 Mar 2023 17:49:31 +0000 Subject: [PATCH] Add contributions CSV export --- app/controllers/contributions_controller.rb | 14 ++++++++++++++ app/models/contribution.rb | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/controllers/contributions_controller.rb b/app/controllers/contributions_controller.rb index 272ceb2..a65daff 100644 --- a/app/controllers/contributions_controller.rb +++ b/app/controllers/contributions_controller.rb @@ -3,10 +3,24 @@ class ContributionsController < ApplicationController before_action :set_member, only: %i[ new create ] before_action :set_contribution, only: %i[ edit update delete destroy ] + def to_csv(collection, keys) + csv = [keys].join(",") + "\n" + collection.each do |item| + csv << keys.map { |k| item.send(k) }.join(",") + "\n" + end + + csv + end + # GET /contributions def index @contributions = Contribution.all.order(payment_on: 'DESC') @contributions = @contributions.select {|c| c.payment_on.year == params[:year].to_i } if params[:year] + + respond_to do |format| + format.html + format.csv { render plain: to_csv(@contributions, %i[payment_on member_number member_display_name amount payment_method]) } + end end # GET /contributions/new diff --git a/app/models/contribution.rb b/app/models/contribution.rb index 4d624fe..0b90eb7 100644 --- a/app/models/contribution.rb +++ b/app/models/contribution.rb @@ -1,4 +1,16 @@ class Contribution < ApplicationRecord belongs_to :member has_paper_trail + + def member_number + member&.number + end + + def member_display_name + member&.display_name + end + + def amount + eurocents + end end