You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

  1. # frozen_string_literal: true
  2. class Admin::AnnouncementsController < Admin::BaseController
  3. before_action :set_announcements, only: :index
  4. before_action :set_announcement, except: [:index, :new, :create]
  5. def index
  6. authorize :announcement, :index?
  7. end
  8. def new
  9. authorize :announcement, :create?
  10. @announcement = Announcement.new
  11. end
  12. def create
  13. authorize :announcement, :create?
  14. @announcement = Announcement.new(resource_params)
  15. if @announcement.save
  16. PublishScheduledAnnouncementWorker.perform_async(@announcement.id) if @announcement.published?
  17. log_action :create, @announcement
  18. redirect_to admin_announcements_path
  19. else
  20. render :new
  21. end
  22. end
  23. def edit
  24. authorize :announcement, :update?
  25. end
  26. def update
  27. authorize :announcement, :update?
  28. if @announcement.update(resource_params)
  29. PublishScheduledAnnouncementWorker.perform_async(@announcement.id) if @announcement.published?
  30. log_action :update, @announcement
  31. redirect_to admin_announcements_path
  32. else
  33. render :edit
  34. end
  35. end
  36. def destroy
  37. authorize :announcement, :destroy?
  38. @announcement.destroy!
  39. UnpublishAnnouncementWorker.perform_async(@announcement.id) if @announcement.published?
  40. log_action :destroy, @announcement
  41. redirect_to admin_announcements_path
  42. end
  43. private
  44. def set_announcements
  45. @announcements = AnnouncementFilter.new(filter_params).results.page(params[:page])
  46. end
  47. def set_announcement
  48. @announcement = Announcement.find(params[:id])
  49. end
  50. def filter_params
  51. params.slice(*AnnouncementFilter::KEYS).permit(*AnnouncementFilter::KEYS)
  52. end
  53. def resource_params
  54. params.require(:announcement).permit(:text, :scheduled_at, :starts_at, :ends_at, :all_day)
  55. end
  56. end