docbrown/app/controllers/admin/sponsorships_controller.rb
yheuhtozr ef290a5120
controllers/admin i18n (#17085)
* admin controllers i18n

* remove ja.yml

* fix for spec

* fix for spec 2

* Apply suggestions from code review

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

* Update config/locales/controllers/admin/fr.yml

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>
2022-04-26 09:40:09 -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] = I18n.t("admin.sponsorships_controller.created")
redirect_to admin_sponsorships_path
else
flash[:danger] = @sponsorship.errors_as_sentence
render :new
end
end
def update
@sponsorship = Sponsorship.find(params[:id])
if @sponsorship.update(sponsorship_params)
flash[:notice] = I18n.t("admin.sponsorships_controller.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] = I18n.t("admin.sponsorships_controller.destroyed")
else
flash[:danger] = I18n.t("admin.sponsorships_controller.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