saucy/app/controllers/contributions_controller.rb

89 lines
2.3 KiB
Ruby

class ContributionsController < ApplicationController
before_action :require_login
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
def new
@contribution = Contribution.new
end
# GET /contributions/1/edit
def edit
end
# POST /contributions
def create
@contribution = @member.contributions.build(contribution_params)
Contribution.transaction do
if @contribution.save
@member.handle_new_contribution(@contribution, params.dig(:contribution, :overriden_expires_on))
@member.reset_status!
@member.regenerate_notifications
redirect_to @member, notice: "Contribution was successfully created."
else
render :new, status: :unprocessable_entity
end
end
end
# PATCH/PUT /contributions/1
def update
if @contribution.update(contribution_params)
redirect_to @contribution.member, notice: "Contribution was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end
#
# GET /contributions/1/delete
def delete
end
# DELETE /contributions/1
def destroy
@member = @contribution.member
@contribution.destroy
redirect_to @member, notice: "Member personal data permanently removed."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_member
@member = Member.find(params[:member_id])
end
def set_contribution
@contribution = Contribution.find(params[:id])
end
# Only allow a list of trusted parameters through.
def contribution_params
params.fetch(:contribution, {}).permit(:eurocents, :payment_method, :payment_on, :payment_reference)
end
end