|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class AnnouncementsController < ApplicationController |
| 4 | + before_action :set_event |
| 5 | + before_action :set_announcement, except: [:index, :new] |
| 6 | + before_action :set_event_follow |
| 7 | + |
| 8 | + def index |
| 9 | + @announcement = Announcement.new |
| 10 | + @announcement.event = @event |
| 11 | + |
| 12 | + authorize @announcement |
| 13 | + |
| 14 | + @all_announcements = Announcement.where(event: @event).order(published_at: :desc, created_at: :desc) |
| 15 | + @announcements = @all_announcements.page(params[:page]).per(10) |
| 16 | + |
| 17 | + raise ActionController::RoutingError.new("Not Found") if !@event.is_public && @all_announcements.empty? && !organizer_signed_in? |
| 18 | + end |
| 19 | + |
| 20 | + def new |
| 21 | + @announcement = Announcement.new |
| 22 | + @announcement.event = @event |
| 23 | + |
| 24 | + authorize @announcement |
| 25 | + end |
| 26 | + |
| 27 | + def create |
| 28 | + @announcement = @event.announcements.build(params.require(:announcement).permit(:title, :content).merge(author: current_user)) |
| 29 | + |
| 30 | + authorize @announcement |
| 31 | + |
| 32 | + @announcement.save! |
| 33 | + |
| 34 | + unless params[:announcement][:draft] == "true" |
| 35 | + @announcement.publish! |
| 36 | + end |
| 37 | + |
| 38 | + flash[:success] = "Announcement successfully #{params[:announcement][:draft] == "true" ? "drafted" : "published"}!" |
| 39 | + |
| 40 | + rescue => e |
| 41 | + puts e.message |
| 42 | + flash[:error] = "Something went wrong. #{e.message}" |
| 43 | + Rails.error.report(e) |
| 44 | + ensure |
| 45 | + redirect_to event_announcement_path(@event, @announcement) |
| 46 | + end |
| 47 | + |
| 48 | + def show |
| 49 | + authorize @announcement |
| 50 | + end |
| 51 | + |
| 52 | + def edit |
| 53 | + authorize @announcement |
| 54 | + |
| 55 | + render "announcements/show", locals: { editing: true } |
| 56 | + end |
| 57 | + |
| 58 | + def update |
| 59 | + authorize @announcement |
| 60 | + |
| 61 | + @announcement.update!(params.require(:announcement).permit(:title, :content)) |
| 62 | + |
| 63 | + if params[:announcement][:autosave] != "true" |
| 64 | + flash[:success] = "Updated announcement" |
| 65 | + redirect_to event_announcement_path(@event, @announcement) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + def destroy |
| 70 | + authorize @announcement |
| 71 | + |
| 72 | + @announcement.destroy! |
| 73 | + |
| 74 | + flash[:success] = "Deleted announcement" |
| 75 | + |
| 76 | + redirect_to event_announcements_path(@event) |
| 77 | + end |
| 78 | + |
| 79 | + def publish |
| 80 | + authorize @announcement |
| 81 | + |
| 82 | + @announcement.publish! |
| 83 | + |
| 84 | + flash[:success] = "Published announcement" |
| 85 | + |
| 86 | + redirect_to event_announcement_path(@event, @announcement) |
| 87 | + end |
| 88 | + |
| 89 | + private |
| 90 | + |
| 91 | + def set_announcement |
| 92 | + if params[:id].present? |
| 93 | + @announcement = @event.announcements.find(params[:id]) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + def set_event |
| 98 | + if params[:event_id].present? |
| 99 | + @event = Event.find_by!(slug: params[:event_id]) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + def set_event_follow |
| 104 | + @event_follow = Event::Follow.where({ user_id: current_user.id, event_id: @event.id }).first if current_user |
| 105 | + end |
| 106 | + |
| 107 | +end |
0 commit comments