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