Sha256: 0f4de639cccf13d45a6ede314c4ffd07dab730388797177e007c08eb31d40cce

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Spina
  module Admin
    class AttachmentsController < AdminController
      before_action :set_breadcrumbs

      def index
        @attachments = Attachment.sorted.with_attached_file.page(params[:page]).per(25)
      end
      
      def show
        @attachment = Attachment.find(params[:id])
      end
      
      def edit
        @attachment = Attachment.find(params[:id])
      end

      def create
        @attachments = params[:attachment][:files].map do |file|
          next if file.blank? # Skip the blank string posted by the hidden files[] field
          
          attachment = Attachment.create(attachment_params)
          attachment.file.attach(file)
          attachment
        end.compact
        
        respond_to do |format|
          format.turbo_stream { render turbo_stream: turbo_stream.prepend("attachments", partial: "attachment", collection: @attachments)}
          format.html { redirect_to spina.admin_attachments_url }
        end
      end
      
      def update
        @attachment = Attachment.find(params[:id])
        if params[:filename].present?
          extension = @attachment.file.filename.extension
          filename = "#{params[:filename]}.#{extension}"
          @attachment.file.blob.update(filename: filename)
        end
        
        redirect_to [:admin, @attachment]
      end

      def destroy
        @attachment = Attachment.find(params[:id])
        @attachment.destroy
        render turbo_stream: turbo_stream.remove(@attachment)
      end

      private

      def set_breadcrumbs
        add_breadcrumb I18n.t('spina.website.media_library')
      end

      def attachment_params
        params.require(:attachment).permit(:file, :page_id, :_destroy)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spina-2.9.1 app/controllers/spina/admin/attachments_controller.rb