docbrown/app/controllers/admin/sponsorships_controller.rb
Fernando Valverde 536b2c821c
Adds ability to create new sponsorships from admin dashboard (#10187)
* Adds create feature to the sponsorships admin dashboard

* Fixes spec
2020-09-03 10:10:37 -06:00

74 lines
2.1 KiB
Ruby

module Admin
class SponsorshipsController < Admin::ApplicationController
layout "admin"
def index
@sponsorships = Sponsorship.includes(:organization, :user, :sponsorable)
.order(created_at: :desc)
.page(params[:page]).per(50)
return if params[:status].blank?
@sponsorships = @sponsorships.where(status: params[:status])
end
def new
@sponsorship = Sponsorship.new
end
def edit
@sponsorship = Sponsorship.find(params[:id])
end
def create
@sponsorship = Sponsorship.new(sponsorship_params)
if @sponsorship.save
flash[:success] = "Sponsorship has been created!"
redirect_to admin_sponsorships_path
else
flash[:danger] = @sponsorship.errors_as_sentence
render new_admin_sponsorship_path
end
end
def update
@sponsorship = Sponsorship.find(params[:id])
if @sponsorship.update(sponsorship_params)
flash[:notice] = "Sponsorship was successfully updated"
redirect_to admin_sponsorships_path
else
flash[:danger] = @sponsorship.errors_as_sentence
render action: :edit
end
end
def destroy
@sponsorship = Sponsorship.find(params[:id])
if @sponsorship.destroy
flash[:notice] = "Sponsorship was successfully destroyed"
else
flash[:danger] = "Sponsorship was not destroyed"
end
redirect_to admin_sponsorships_path
end
private
def sponsorship_params
strong_params = params.fetch(:sponsorship, {})
.permit(:status, :expires_at, :tagline, :url,
:blurb_html, :featured_number,
:instructions, :level, :user_id,
:sponsorable_id, :sponsorable_type,
:organization_id, :instructions_updated_at)
if strong_params[:sponsorable_id].try(:empty?) || strong_params[:sponsorable_type].try(:empty?)
# Clear sponsorable & sponsorable_type if they were left empty
strong_params.delete("sponsorable_id")
strong_params.delete("sponsorable_type")
end
strong_params
end
end
end