Add contributions CSV export

main
Hugo Peixoto 3 months ago
parent a1be74ed2d
commit 318b7878d8
  1. 14
      app/controllers/contributions_controller.rb
  2. 12
      app/models/contribution.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

@ -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

Loading…
Cancel
Save