Add contributions CSV export

This commit is contained in:
Hugo Peixoto 2023-03-19 17:49:31 +00:00
parent a1be74ed2d
commit 318b7878d8
2 changed files with 26 additions and 0 deletions

View File

@ -3,10 +3,24 @@ class ContributionsController < ApplicationController
before_action :set_member, only: %i[ new create ] before_action :set_member, only: %i[ new create ]
before_action :set_contribution, only: %i[ edit update delete destroy ] 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 # GET /contributions
def index def index
@contributions = Contribution.all.order(payment_on: 'DESC') @contributions = Contribution.all.order(payment_on: 'DESC')
@contributions = @contributions.select {|c| c.payment_on.year == params[:year].to_i } if params[:year] @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 end
# GET /contributions/new # GET /contributions/new

View File

@ -1,4 +1,16 @@
class Contribution < ApplicationRecord class Contribution < ApplicationRecord
belongs_to :member belongs_to :member
has_paper_trail has_paper_trail
def member_number
member&.number
end
def member_display_name
member&.display_name
end
def amount
eurocents
end
end end