class Admin::DonationsController < Admin::BaseController before_action :set_donation, only: [:show, :edit, :update, :destroy] before_action :set_current_section, only: [:index, :show, :new, :edit] # GET /donations def index @username = params[:username].presence pending_scope = Donation.incomplete.joins(:user).order('paid_at desc') completed_scope = Donation.completed.joins(:user).order('paid_at desc') if @username pending_scope = pending_scope.where(users: { cn: @username }) completed_scope = completed_scope.where(users: { cn: @username }) end @pending_donations = pending_scope @pagy, @donations = pagy(completed_scope) @stats = { overall_sats: completed_scope.sum("amount_sats"), donor_count: completed_scope.distinct.count(:user_id) } end # GET /donations/1 def show end # GET /donations/new def new @donation = Donation.new end # GET /donations/1/edit def edit end # POST /donations def create @donation = Donation.new(donation_params) if @donation.paid_at == nil @donation.errors.add(:paid_at, message: "is required") render :new, status: :unprocessable_entity and return end if @donation.save redirect_to admin_donation_url(@donation), flash: { success: 'Donation was successfully created.' } else render :new, status: :unprocessable_entity end end # PUT /donations/1 def update if @donation.update(donation_params) redirect_to admin_donation_url(@donation), flash: { success: 'Donation was successfully updated.' } else render :edit, status: :unprocessable_entity end end # DELETE /donations/1 def destroy @donation.destroy redirect_to admin_donations_url, flash: { success: 'Donation was successfully destroyed.' } end private # Use callbacks to share common setup or constraints between actions. def set_donation @donation = Donation.find(params[:id]) end # Only allow a list of trusted parameters through. def donation_params params.require(:donation).permit( :user_id, :donation_method, :amount_sats, :fiat_amount, :fiat_currency, :public_name, :paid_at) end def set_current_section @current_section = :donations end end