Sha256: 67b287a9474863252424aec6db9abefaf09e537a413defc486056ed5c7b4d4fd

Contents?: true

Size: 1.06 KB

Versions: 13

Compression:

Stored size: 1.06 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]
      association_name = BaseResource.valid_attachment_name(@model, params[:attachment_key])

      @model.send(association_name).attach blob

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

    def destroy
      attachment = ActiveStorage::Attachment.find(params[:attachment_id])
      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

13 entries across 13 versions & 1 rubygems

Version Path
avo-2.21.1.pre.issue1450 app/controllers/avo/attachments_controller.rb
avo-2.21.1.pre.issue1444 app/controllers/avo/attachments_controller.rb
avo-2.20.0 app/controllers/avo/attachments_controller.rb
avo-2.19.0 app/controllers/avo/attachments_controller.rb
avo-2.18.1 app/controllers/avo/attachments_controller.rb
avo-2.18.1.pre.1.eagerloaddirs app/controllers/avo/attachments_controller.rb
avo-2.18.0 app/controllers/avo/attachments_controller.rb
avo-2.17.1.pre.5.stackedlayout app/controllers/avo/attachments_controller.rb
avo-2.17.1.pre.4.issue.1342 app/controllers/avo/attachments_controller.rb
avo-2.17.1.pre.3 app/controllers/avo/attachments_controller.rb
avo-2.17.1.pre.2.customauthorizationclients app/controllers/avo/attachments_controller.rb
avo-2.17.1.pre.1.zeitwerk.eager.load.dir app/controllers/avo/attachments_controller.rb
avo-2.17.0 app/controllers/avo/attachments_controller.rb