Sha256: 2a36fa447d304f248454ea76f8f408743680d4c78f2dd9c0f878764b252ce7b0

Contents?: true

Size: 1.09 KB

Versions: 33

Compression:

Stored size: 1.09 KB

Contents

require_dependency "avo/application_controller"

module Avo
  class AttachmentsController < ApplicationController
    before_action :set_resource_name, only: [:destroy, :create]
    before_action :set_resource, only: [:destroy, :create]
    before_action :set_model, only: [:destroy, :create]

    def create
      blob = ActiveStorage::Blob.create_and_upload! io: params[:file], filename: params[:filename]

      @model.send(params[:attachment_key]).attach blob

      render json: {
        url: main_app.url_for(blob),
        href: main_app.url_for(blob)
      }
    end

    def show
    end

    def destroy
      blob = ActiveStorage::Blob.find(params[:signed_attachment_id])
      attachment = blob.attachments.find_by record_id: params[:id], record_type: @model.class.to_s
      path = resource_path(model: @model, resource: @resource)

      if attachment.present?
        attachment.destroy

        redirect_to params[:referrer] || path, notice: t("avo.attachment_destroyed")
      else
        redirect_back fallback_location: path, notice: t("avo.failed_to_find_attachment")
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
avo-1.25.2 app/controllers/avo/attachments_controller.rb
avo-1.25.1 app/controllers/avo/attachments_controller.rb
avo-1.25.0 app/controllers/avo/attachments_controller.rb
avo-1.24.2 app/controllers/avo/attachments_controller.rb
avo-1.24.1 app/controllers/avo/attachments_controller.rb
avo-1.24.0 app/controllers/avo/attachments_controller.rb
avo-1.23.0 app/controllers/avo/attachments_controller.rb
avo-1.22.4 app/controllers/avo/attachments_controller.rb
avo-1.22.3 app/controllers/avo/attachments_controller.rb
avo-1.22.2 app/controllers/avo/attachments_controller.rb
avo-1.22.1 app/controllers/avo/attachments_controller.rb
avo-1.22.1.pre.2 app/controllers/avo/attachments_controller.rb
avo-1.22.1.pre.1 app/controllers/avo/attachments_controller.rb
avo-1.22.0 app/controllers/avo/attachments_controller.rb
avo-1.22.0.pre.1 app/controllers/avo/attachments_controller.rb
avo-1.21.1.pre.1 app/controllers/avo/attachments_controller.rb
avo-1.21.0 app/controllers/avo/attachments_controller.rb
avo-1.20.2.pre.2 app/controllers/avo/attachments_controller.rb
avo-1.20.2.pre.1 app/controllers/avo/attachments_controller.rb
avo-1.21.0.pre.1 app/controllers/avo/attachments_controller.rb