2022-01-24 12:46:10 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-19 12:12:22 -07:00
|
|
|
class AnnouncementController < ApplicationController
|
2020-04-19 13:45:07 -07:00
|
|
|
before_action :authenticate_user!
|
|
|
|
|
2020-04-19 12:12:22 -07:00
|
|
|
def index
|
|
|
|
@announcements = Announcement.all
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2020-04-19 12:34:48 -07:00
|
|
|
@announcement = Announcement.new
|
2020-04-19 12:12:22 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-04-19 12:34:48 -07:00
|
|
|
@announcement = Announcement.new(announcement_params)
|
|
|
|
@announcement.user = current_user
|
|
|
|
if @announcement.save
|
2022-01-24 12:46:10 -08:00
|
|
|
flash[:success] = t(".success")
|
2020-04-19 12:34:48 -07:00
|
|
|
redirect_to action: :index
|
|
|
|
else
|
2022-01-24 12:46:10 -08:00
|
|
|
flash[:error] = t(".error")
|
|
|
|
render "announcement/new"
|
2020-04-19 12:34:48 -07:00
|
|
|
end
|
2020-04-19 12:12:22 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2020-04-19 12:50:33 -07:00
|
|
|
@announcement = Announcement.find(params[:id])
|
2020-04-19 12:12:22 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2020-04-19 13:26:55 -07:00
|
|
|
@announcement = Announcement.find(params[:id])
|
|
|
|
@announcement.update(announcement_params)
|
|
|
|
if @announcement.save
|
2022-01-24 13:17:53 -08:00
|
|
|
flash[:success] = t(".success")
|
2020-04-19 12:58:57 -07:00
|
|
|
redirect_to announcement_index_path
|
|
|
|
else
|
2022-01-24 13:17:53 -08:00
|
|
|
flash[:error] = t(".error")
|
2022-01-24 12:46:10 -08:00
|
|
|
render "announcement/edit"
|
2020-04-19 12:58:57 -07:00
|
|
|
end
|
2020-04-19 12:12:22 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2020-04-19 12:50:33 -07:00
|
|
|
if Announcement.destroy(params[:id])
|
2022-01-24 12:46:10 -08:00
|
|
|
flash[:success] = t(".success")
|
2020-04-19 12:50:33 -07:00
|
|
|
else
|
2022-01-24 12:46:10 -08:00
|
|
|
flash[:error] = t(".error")
|
2020-04-19 12:50:33 -07:00
|
|
|
end
|
|
|
|
redirect_to announcement_index_path
|
2020-04-19 12:12:22 -07:00
|
|
|
end
|
2020-04-19 12:34:48 -07:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def announcement_params
|
|
|
|
params.require(:announcement).permit(:content, :link_text, :link_href, :starts_at, :ends_at)
|
|
|
|
end
|
2020-04-19 12:12:22 -07:00
|
|
|
end
|