Skip to content

Instantly share code, notes, and snippets.

@nightcrawler-
Created January 16, 2022 09:58
Show Gist options
  • Save nightcrawler-/9e32eadd3cd89dddced015202bc02361 to your computer and use it in GitHub Desktop.
Save nightcrawler-/9e32eadd3cd89dddced015202bc02361 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class ContributionsController < ApplicationController
skip_before_action :authenticate_user!, only: %i[new create show] # Allow the new contributions page to be accessible without sign in: for sharing OGP data, search engine indexing etc. Also allow create, so no regostration required to contribute, jst phone number
before_action :set_contribution, only: %i[show edit update destroy]
before_action :set_campaign, only: %i[new create]
# GET /contributions
# GET /contributions.json
def index
@contributions = current_user.contributions
end
# GET /contributions/1
# GET /contributions/1.json
def show; end
# GET /contributions/new
def new
@contribution = Contribution.new
end
# GET /contributions/1/edit
def edit; end
# POST /contributions
# POST /contributions.json
def create
puts current_user.inspect
@contribution = Contribution.new(contribution_params)
@contribution.contributor_id = if current_user.present?
current_user.id
else
User.from_phone_number(contribution_params).id
end
respond_to do |format|
if @contribution.save
format.html { redirect_to @contribution, notice: 'Contribution was successfully created.' }
format.json { render :show, status: :created, location: @contribution }
else
format.html { render :new }
format.json { render json: @contribution.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /contributions/1
# PATCH/PUT /contributions/1.json
def update
respond_to do |format|
if @contribution.update(contribution_params)
format.html { redirect_to @contribution, notice: 'Contribution was successfully updated.' }
format.json { render :show, status: :ok, location: @contribution }
else
format.html { render :edit }
format.json { render json: @contribution.errors, status: :unprocessable_entity }
end
end
end
# DELETE /contributions/1
# DELETE /contributions/1.json
def destroy
@contribution.destroy
respond_to do |format|
format.html { redirect_to contributions_url, notice: 'Contribution was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_contribution
@contribution = authorize Contribution.find(params[:id])
end
def set_campaign
id = params[:campaign_id] # get as query param (when new action)
begin
id ||= contribution_params[:campaign_id] # get as form param (when create action)
rescue StandardError
end
@campaign = Campaign.find(id) if id
# Go to home if no campaign is available
redirect_to '/' unless @campaign
end
# Only allow a list of trusted parameters through.
def contribution_params
params.require(:contribution).permit(:amount, :source, :message, :phone_number, :campaign_id, :contributor_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment