module Admin class AnnouncementsController < Admin::BaseController before_action :set_announcement, only: %i[edit update destroy] def index @announcements = AppAnnouncement.newest end def new @announcement = AppAnnouncement.new( kind: "info", severity: "info", status: "draft", show_on_android: true, show_on_ios: true, show_on_web_public: true, show_on_web_private: true, dismissible: true ) end def create @announcement = AppAnnouncement.new(announcement_params) @announcement.created_by_admin = current_admin_account if @announcement.save redirect_to admin_announcements_path, notice: t("admin.flash.announcement_created") else flash.now[:alert] = @announcement.errors.full_messages.join(", ") render :new, status: :unprocessable_entity end end def edit; end def update if @announcement.update(announcement_params) redirect_to admin_announcements_path, notice: t("admin.flash.announcement_updated") else flash.now[:alert] = @announcement.errors.full_messages.join(", ") render :edit, status: :unprocessable_entity end end def destroy @announcement.destroy! redirect_to admin_announcements_path, notice: t("admin.flash.announcement_destroyed") end private def set_announcement @announcement = AppAnnouncement.find(params[:id]) end def announcement_params params.require(:app_announcement).permit( :kind, :severity, :status, :title, :body, :action_url, :action_label, :dismissible, :starts_at, :ends_at, :show_on_android, :show_on_ios, :show_on_web_public, :show_on_web_private, :android_title, :android_body, :android_action_url, :android_action_label, :ios_title, :ios_body, :ios_action_url, :ios_action_label ).tap do |permitted| %i[dismissible show_on_android show_on_ios show_on_web_public show_on_web_private].each do |key| permitted[key] = ActiveModel::Type::Boolean.new.cast(permitted[key]) end %i[ starts_at ends_at action_url action_label android_title android_body android_action_url android_action_label ios_title ios_body ios_action_url ios_action_label ].each do |key| permitted[key] = nil if permitted[key].blank? end end end end end